Does INT() Function do rounding

Moderator: Rathinagiri

Post Reply
cherianj
Posts: 12
Joined: Wed Feb 24, 2016 3:03 am

Does INT() Function do rounding

Post by cherianj »

I have a problem with the INT() function. With the following code. I expect a result of 86, but what I get is 85.

Num := 76.86
? INT((Num - INT(Num) ) * 100) // Expecting 86 but what I get is 85

Is it because of rounding ?. The Clipper manual says INT() does not do rounding. For checking I split the code as below.

Num := 76.86
Dec := (Num - INT(Num)) * 100
? Dec
IDec := INT(Dec)
? IDec

The value of Dec is shown as 86.00 However IDec is 85. Can anyone help me understand the issue please.

I use HMG 3.4.3 on Windows 10 64 bit.

Regards,

Cherian
User avatar
serge_girard
Posts: 3167
Joined: Sun Nov 25, 2012 2:44 pm
DBs Used: 1 MySQL - MariaDB
2 DBF
Location: Belgium
Contact:

Re: Does INT() Function do rounding

Post by serge_girard »

Try this:

msginfo(str ((Num - INT(Num)) * 100) )


Serge
There's nothing you can do that can't be done...
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Does INT() Function do rounding

Post by esgici »

cherianj wrote:I have a problem with the INT() function. With the following code. I expect a result of 86, but what I get is 85.

Num := 76.86
? INT((Num - INT(Num) ) * 100) // Expecting 86 but what I get is 85

Is it because of rounding ?. The Clipper manual says INT() does not do rounding. For checking I split the code as below.

Num := 76.86
Dec := (Num - INT(Num)) * 100
? Dec
IDec := INT(Dec)
? IDec

The value of Dec is shown as 86.00 However IDec is 85. Can anyone help me understand the issue please.

I use HMG 3.4.3 on Windows 10 64 bit.

Regards,

Cherian
Hi Cherian

First you are welcome wonderful word of HMG :)

And second, this is a Harbour issue, not HMG; because INT() is a Harbour function.

Added later:

Harbour ISSUE; not PROBLEM as understood wrongly :(

However you are in correct place, don't worry.

Regarding your question:

please look at full description of function, you will see no difference found between Clipper and Harbour at this point:
Description

This function converts a numeric expression to an integer. All decimal digits are truncated. This function does not round a value upward or downward; it merely truncates a number at the decimal point.
So getting decimal fraction as a integer of any number is easy than you imagine:

Code: Select all

Num := 76.86
IDec := (Num - INT(Num)) * 100
That's all :arrow:

Happy HMG'ing :D
Last edited by esgici on Sat Aug 27, 2016 12:42 pm, edited 1 time in total.
Viva INTERNATIONAL HMG :D
cherianj
Posts: 12
Joined: Wed Feb 24, 2016 3:03 am

Re: Does INT() Function do rounding

Post by cherianj »

Thank you for the quick response. I checked the manual and tried different methods. It does not give the correct value always. As you rightly said, its a Harbour issue. I managed to solve by using Dec := VAL(RIGHT(STR(Num),2)).

Serge,

msginfo(str ((Num - INT(Num)) * 100) Shows correct value with decimals (86.00), when i take INT() of it, it goes wrong (85).

Thank you all.

Cherian
PeteWG
Posts: 176
Joined: Sun Mar 21, 2010 5:45 pm

Re: Does INT() Function do rounding

Post by PeteWG »

Hi,

This is NOT Harbour problem (as wrongly said in a post above).

It's a known (?) "issue", in most of programming languages, due to precision loss when performing floating-point arithmetic.

If you want to get a clue what's exactly happening, try the following code:

------------------------------------
PROC MAIN()

LOCAL Num := 76.86
SET DECIMAL TO 14
SET FIXED on
? ( Num - INT(Num) ) * 100
SET FIXED OFF
? Int( ( Num - INT(Num) ) * 100 )
// how to overcome?
// as already suggested convert to string and back to numeric

? Int( Val( hb_ntos( ( Num - INT(Num) ) * 100 ) ) )

-------------------------------------

[ further possibly useful reading Floating point inaccuracy examples ]

regards,

---
Pete
cherianj
Posts: 12
Joined: Wed Feb 24, 2016 3:03 am

Re: Does INT() Function do rounding

Post by cherianj »

Thanks you for the clarification Pete.

Regards,

Cherian
Post Reply