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

Mailing lists / fpc-pascal / 2018-05
8 messages · 5 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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
int(x) is probably the better way because frac calculates int(x) first and then subtracts this part from x (basically).
after all these discussions about the frac function, what is the best way to test if…
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
Jonas Maebe wroteThe 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
The only proper way is to use something like Math.SameValue(x, int(x)). Never use…
Jonas Maebe wroteThe 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
Bart schrieb am Di., 15. Mai 2018, 13:22:
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
Just curious. If Frac(x) equals zero, will Int(x) cahnge the bitpattern of x then? (Or:…
Bart wroteIf 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?)
That cannot be guaranteed.
Jonas
OK.
Bart