Page 1 of 1

FRACT of Zero

Posted: Mon Apr 11, 2022 10:19 am
by Erg2
'Hi Mr Kibernetik,

'why is this happening?

a=7.1
print integ(a) 'answer is 7, all good

b=fract(a)*60 'that is, 0.1 * 60 = 6
print b 'answer is 6, all good

print fract(b) 'displaying answer as 1

'if b=6 then why is it that fract(b) equals 1, should it not be zero?

Thanks

Re: FRACT of Zero

Posted: Mon Apr 11, 2022 11:42 am
by Mr. Kibernetik
Yes, this is an interesting case. This happens because float numbers are not exact.

When you assign
a=7.1
internally 'a' becomes 7.09999999...
So, 'b' becomes 5.99999999...

PRINT function handles this problem, so
print b
gives you '6' instead of 5.99999999

But
print fract (b)
gives you '1' instead of 0.99999999...

The same, if you will
print integ (b)
you will get '5'.

This problem occurs because smart BASIC uses only float values, even if you expect to get integer result.
You can use
b = int(b)
to ensure that 'b' is really an integer value now.