Mysterious counting of the INT() function

Issues and Discussions related to Harbour

Moderator: Rathinagiri

Post Reply
edk
Posts: 387
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 114 times
Been thanked: 287 times

Mysterious counting of the INT() function

Post by edk » Tue Apr 10, 2018 1:14 pm

To all.
Do you also get the number 5 instead of the expected 6 as a result of

Code: Select all

msgdebug( INT( ( 129.6 - 129 ) * 10 ) )
:?: :!: :shock:

Edward.

User avatar
apais
Posts: 286
Joined: Fri Aug 01, 2008 6:03 pm
Location: uruguay
Has thanked: 36 times
Been thanked: 22 times
Contact:

Post by apais » Tue Apr 10, 2018 1:31 pm

floating point issues !

User avatar
serge_girard
Posts: 2166
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Has thanked: 472 times
Been thanked: 108 times
Contact:

Post by serge_girard » Tue Apr 10, 2018 2:04 pm

Yes, me too...

Serge

User avatar
SALINETAS24
Posts: 231
Joined: Tue Feb 27, 2018 3:06 am
DBs Used: DBF
Has thanked: 5 times
Been thanked: 6 times

Post by SALINETAS24 » Mon Apr 16, 2018 7:16 pm

msgdebug( INT( VAL(STR(( 129.6 - 129 ) * 10 ) )))

Raro, raro .., raro

User avatar
Anand
Posts: 321
Joined: Tue May 24, 2016 4:36 pm
DBs Used: DBF
Has thanked: 64 times
Been thanked: 44 times

Post by Anand » Tue Apr 17, 2018 10:00 am

We are using str() with default values of decimal points and again val() to convert it, so problem can come up at random times. I am not sure when and why, but have faced such situation in Clipper and Xbase++ many times.

If it can be avoided then use int((129.6-129)*10), else use round(x,n) to keep the decimals at known places.

Regards,

Anand
Image

edk
Posts: 387
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland
Has thanked: 114 times
Been thanked: 287 times

Post by edk » Tue Apr 17, 2018 1:09 pm

As Angel points out
apais wrote:
Tue Apr 10, 2018 1:31 pm
floating point issues !
this is a problem with the binary format of floating point numbers: https://harbour.github.io/the-oasis/clipper-5.html

Using integers in the calculations performed, solves the problem.

Code: Select all

msgdebug( INT( ( 129.6 - 129 ) * 10 ) )   vs.   msgdebug( INT(  129.6*10 - 129*10 ) )

Post Reply