Page 1 of 1
Does INT() Function do rounding
Posted: Fri Aug 26, 2016 1:37 am
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
Re: Does INT() Function do rounding
Posted: Fri Aug 26, 2016 5:38 am
by serge_girard
Try this:
msginfo(str ((Num - INT(Num)) * 100) )
Serge
Re: Does INT() Function do rounding
Posted: Fri Aug 26, 2016 5:46 am
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
Happy HMG'ing

Re: Does INT() Function do rounding
Posted: Fri Aug 26, 2016 7:53 am
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
Re: Does INT() Function do rounding
Posted: Fri Aug 26, 2016 8:45 am
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
Re: Does INT() Function do rounding
Posted: Sat Aug 27, 2016 11:36 am
by cherianj
Thanks you for the clarification Pete.
Regards,
Cherian