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
Does INT() Function do rounding
Moderator: Rathinagiri
- 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
Try this:
msginfo(str ((Num - INT(Num)) * 100) )
Serge
msginfo(str ((Num - INT(Num)) * 100) )
Serge
There's nothing you can do that can't be done...
- esgici
- Posts: 4543
- Joined: Wed Jul 30, 2008 9:17 pm
- DBs Used: DBF
- Location: iskenderun / Turkiye
- Contact:
Re: Does INT() Function do rounding
Hi Cheriancherianj 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
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:
So getting decimal fraction as a integer of any number is easy than you imagine: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.
Code: Select all
Num := 76.86
IDec := (Num - INT(Num)) * 100
Happy HMG'ing
Last edited by esgici on Sat Aug 27, 2016 12:42 pm, edited 1 time in total.
Viva INTERNATIONAL HMG
Re: Does INT() Function do rounding
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
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
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
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
Thanks you for the clarification Pete.
Regards,
Cherian
Regards,
Cherian