Sign in Join

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

Mailing lists / fpc-pascal / 2014-12

compare currency and double

5 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Xiangrong Fang

Hi All,

I have the following code:

var
  val: Currency;
  res: Double;
begin
  val := 2345.67;
  res := 1e300;
  if val >= res then WriteLn('val >=res')
  else WriteLn('val < res');
end;

The program compile and runs correctly on Linux x64, and Win7 32bit, but failed (SIGFPE) on Win7 64bit.

Can I safely compare a Currency against a Double?

Thanks!

Xiangrong

Reply by email

Jonas Maebe
to Xiangrong Fang
Xiangrong Fang wrote 14 lines
I have the following code: var val: Currency; res: Double; begin val := 2345.67; res := 1e300; if val >= res then WriteLn('val >=res') else WriteLn('val < res'); end; The program compile and runs correctly on Linux x64, and Win7 32bit, but failed (SIGFPE) on Win7 64bit.

That's because currency has a bigger range than double. On Linux x64 and Windows 32 bit, the x87 fpu can be used, so the currency value can be converted to an 80 bits extended value. The x87 and hence also its 80 bits extended type have been deprecated on Win64, so FPC doesn't use it there anymore.

Can I safely compare a Currency against a Double?

No. It generally also doesn't make sense, because the whole point of using currency is to have an exact value without rounding errors, while a double value by nature very often contains rounding errors.

Jonas

Reply by email

Xiangrong Fang
to Jonas Maebe

Thank you! I have changed all occurrences of Currency to Double in my program.

2014-12-08 21:25 GMT+08:00 Jonas Maebe :

Show quoted text 9 lines
On 07 Dec 2014, at 16:07, Jonas Maebe wrote: That's because currency has a bigger range than double. > Or rather: a double cannot exactly represent every possible currency value.

Reply by email

Joost van der Sluis
to Xiangrong Fang

Ehm....

The question is if that's the right approach.

And if you didn't top-post your message it was now immediately clear what I mean with this. Now you've created a little puzzle for yourself...

Regards,

Joost.

Xiangrong Fang wrote 21 lines
Thank you! I have changed all occurrences of Currency to Double in my program. 2014-12-08 21:25 GMT+08:00 Jonas Maebe < >: On 07 Dec 2014, at 16:07, Jonas Maebe wrote: That's because currency has a bigger range than double. Or rather: a double cannot exactly represent every possible currency value. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email