Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-03

float calculates rubbish

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Marc Santhoff

Hi,

i was confused and defined some real values as float in a GTK program. GDK (glib.pp) defines float as single. Look what happened:

program testlongfloat;
uses sysutils, glib;
const
	c1: float = 1109081270;
	c2: float = 1109081270 / 2;
BEGIN
	writeln(inttostr(trunc((c1))));
	writeln(floattostrf(c1,fffixed,8,2));
	writeln(inttostr(trunc((c2))));
	writeln(floattostrf(c2,fffixed,8,2));
END.

$ ./testlongfloat
1109081216
1109081216.00
554540608
554540608.00

Am I producing a value overflow here or what's happening? The whole thing is running on i386 with fpc 1.9.4.

I do not know if single is equivalent to extended on i386 or not.

Next time I'll use an appropriate type declaration from the start, btw. ;)

Thanks,
Marc

Reply by email

Marco van de Voort
to Marc Santhoff
Show quoted text 8 lines
$ ./testlongfloat 1109081216 1109081216.00 554540608 554540608.00 Am I producing a value overflow here or what's happening? The whole thing is running on i386 with fpc 1.9.4.

glib redefined float as 32-bit single, which has much lower precision, which in turn causes the rounding.

Try recompiling the above test without glib, but with math (which contains the Delphi/FPC float definition).

Reply by email

Marc Santhoff
to Marco van de Voort
Show quoted text 14 lines
> $ ./testlongfloat > 1109081216 > 1109081216.00 > 554540608 > 554540608.00 > > Am I producing a value overflow here or what's happening? The whole > thing is running on i386 with fpc 1.9.4. glib redefined float as 32-bit single, which has much lower precision, which in turn causes the rounding. Try recompiling the above test without glib, but with math (which contains the Delphi/FPC float definition).

Ah, I see. And the good one (math) wins, if its last in the row, so if I want to use anything the way it is, I can add the math unit as last in the row or at least after gdk and it'll work as expected in its number range.

Maybe gdk will have some problems where it is expects float aka single as parameter and is fed with double or extended...

Thanks,
Marc

Reply by email