FRACT of Zero

Post Reply
Erg2
Posts: 18
Joined: Sat Dec 10, 2016 8:58 pm
My devices: ipad2
Flag: Australia

FRACT of Zero

Post 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

User avatar
Mr. Kibernetik
Site Admin
Posts: 4786
Joined: Mon Nov 19, 2012 10:16 pm
My devices: iPhone, iPad, MacBook
Location: Russia
Flag: Russia

Re: FRACT of Zero

Post 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.

Post Reply