Mysterious counting of the INT() function

Issues and Discussions related to Harbour

Moderator: Rathinagiri

Post Reply
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Mysterious counting of the INT() function

Post by edk »

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: 440
Joined: Fri Aug 01, 2008 6:03 pm
DBs Used: DBF
Location: uruguay
Contact:

Re: Mysterious counting of the INT() function

Post by apais »

floating point issues !
Angel Pais
Web Apps consultant/architect/developer.
HW_apache (webserver modules) co-developer.
HbTron (Html GUI for harbour desktop hybrid apps) co-developer.
https://www.hbtron.com
User avatar
serge_girard
Posts: 3158
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Mysterious counting of the INT() function

Post by serge_girard »

Yes, me too...

Serge
There's nothing you can do that can't be done...
User avatar
SALINETAS24
Posts: 667
Joined: Tue Feb 27, 2018 3:06 am
DBs Used: DBF
Contact:

Re: Mysterious counting of the INT() function

Post by SALINETAS24 »

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

Raro, raro .., raro
Como dijo el gran pensador Hommer Simpson..., - En este mundo solo hay 3 tipos de personas, los que saben contar y los que no. :shock:
User avatar
Anand
Posts: 595
Joined: Tue May 24, 2016 4:36 pm
DBs Used: DBF

Re: Mysterious counting of the INT() function

Post by Anand »

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
Regards,

Anand

Image
edk
Posts: 909
Joined: Thu Oct 16, 2014 11:35 am
Location: Poland

Re: Mysterious counting of the INT() function

Post by edk »

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