Sign in Join

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

Mailing lists / fpc-pascal / 2018-05

testing if a float has a fractional part

8 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

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

Benito van der Zander

Hi,

after all these discussions about the frac function, what is the best way to test if there is a fractional part? That is the most common thing I use frac for

I have always used frac(x) = 0

Is that worse or better than int(x) = x  ?

Benito

Reply by email

Florian Klämpfl
to Benito van der Zander
Benito van der Zander wrote 8 lines
Hi, after all these discussions about the frac function, what is the best way to test if there is a fractional part? That is the most common thing I use frac for I have always used frac(x) = 0 Is that worse or better than int(x) = x  ?

int(x) is probably the better way because frac calculates int(x) first and then subtracts this part from x (basically).

Reply by email

Jonas Maebe
to Benito van der Zander: after all these discussions about the frac function, what is the best way to test if…
Benito van der Zander wrote 7 lines
after all these discussions about the frac function, what is the best way to test if there is a fractional part? That is the most common thing I use frac for I have always used frac(x) = 0 Is that worse or better than int(x) = x  ?

The only proper way is to use something like Math.SameValue(x, int(x)). Never use equality tests with floating point values, unless you are checking for a specific bit pattern (or if you know that the value can always be represented exactly a sum of power-of-two's).

Jonas

Reply by email

Bart
to Jonas Maebe
Jonas Maebe wrote
The only proper way is to use something like Math.SameValue(x, int(x)). Never use equality tests with floating point values, unless you are checking for a specific bit pattern (or if you know that the value can always be represented exactly a sum of power-of-two's).

Just curious.

If Frac(x) equals zero, will Int(x) cahnge the bitpattern of x then? (Or: it cannot be gueranteed that this will not be the case?)

Bart

Reply by email

Bart
to Jonas Maebe: The only proper way is to use something like Math.SameValue(x, int(x)). Never use…
Jonas Maebe wrote
The only proper way is to use something like Math.SameValue(x, int(x)).

B.t.w. the test suite for the new Int() function (win64) (https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/tests/tbs/tb0644.pp?view=markup) uses

47 function SameValue(aGot, aExpected: Double): Boolean;
48 begin
49 if IsNan(aExpected) then
50 Result := IsNan(aGot)
51 else
52 Result := aGot = aExpected;
53 end;

Bart

Reply by email

Sven Barth
to Bart

Bart schrieb am Di., 15. Mai 2018, 13:22:

Show quoted text 17 lines
On Sun, May 13, 2018 at 3:40 PM, Jonas Maebe wrote: > The only proper way is to use something like Math.SameValue(x, int(x)). B.t.w. the test suite for the new Int() function (win64) ( https://svn.freepascal.org/cgi-bin/viewvc.cgi/trunk/tests/tbs/tb0644.pp?view=markup ) uses 47 function SameValue(aGot, aExpected: Double): Boolean; 48 begin 49 if IsNan(aExpected) then 50 Result := IsNan(aGot) 51 else 52 Result := aGot = aExpected; 53 end;

But that test tests a specific functionality and even remarks that it will only work on targets that have Double as default floating point type (e.g. Win64 and non-x86 targets). That test would fail on targets that have the Extended type or only Single.
Thus it is safer (and more portable) to use Math.SameValue().

Regards,
Sven

Reply by email