Sign in to follow this  
Followers 0
is8591

Is this a bug in INT() ?

13 posts in this topic

I ran into a small problem using Int().

Simple formula $result = Int($i /100 * 100) should always return $result equal to $i but it does not.

So i made a simple script to run through numbers and see when it fails - see code. Result goes into a file.

Int() fails consistently but on random numbers - at least I don't see a pattern.

For better reading I added a colum $i /100 *100 - I thought precision was messing up but $i /100*100 always returns $i.

I hope somebody can explain it.

$file = FileOpen("TestErrors.txt", 2)
For $i = 1 To 999
    If $i <> Int($i/100 * 100) Then
        $s_err = $i & @TAB & $i/100*100 & @TAB & Int($i/100 * 100)
        FileWriteLine($file, $s_err)
    EndIf
Next
FileClose($file)
Exit

Share this post


Link to post
Share on other sites



I hope somebody can explain it.

$file = FileOpen("TestErrors.txt", 2)
For $i = 1 To 999
    If $i <> Int($i/100 * 100) Then
        $s_err = $i & @TAB & $i/100*100 & @TAB & Int($i/100 * 100)
        FileWriteLine($file, $s_err)
    EndIf
Next
FileClose($file)
Exit
I get a testerrors.txt file of ZERO bytes. Indicating no errors were generated.

Share this post


Link to post
Share on other sites

What version ????

Try latest BETA and see if that fixes it...

I am at work - only have 3.1.1 - not allowed to use beta

But I looked through bug reports before posting and did not find any reference Int().

Is there a workaround in 3.1.1 because I will not be able to switch to Beta nayway?

Share this post


Link to post
Share on other sites

But I looked through bug reports before posting and did not find any reference Int().

Is there a workaround in 3.1.1 because I will not be able to switch to Beta nayway?

I get no errors in 3.1.1

or 105 beta

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

I am at work - only have 3.1.1 - not allowed to use beta

But I looked through bug reports before posting and did not find any reference Int().

Is there a workaround in 3.1.1 because I will not be able to switch to Beta nayway?

Think this one is bugging you with the 3.1.10 version fixed in BETA:

07th November, 2005 - v3.1.1.87 (beta)

Fixed : @DesktopHeight/Width doc typo. (Thanks PartyPooper)

Added : VC8 compilation support. (by Valik)

Fixed : Float to Integer precision. (Thanks kalavo, fixed by Valik)

I get no errors in 3.1.1

or 105 beta

I get the errors listed in testerrors.txt when running with 3.1.1.0 Edited by JdeB

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I get no errors in 3.1.1

or 105 beta

My error file attached.

TestErrors.txt

Share this post


Link to post
Share on other sites

think this fixes it for 3.1.1.0 :

$file = FileOpen("TestErrors.txt", 2)

For $i = 1 To 999

If $i <> Int($i/100 * 100 + .0000000000001) Then

$s_err = $i & @TAB & $i/100*100 & @TAB & Int($i/100 * 100)

FileWriteLine($file, $s_err)

EndIf

Next

FileClose($file)

Exit


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

My error file attached.

My Bad.

I wasn't correctly toggling between 1.1 and 1.1.105.

I get the errors now.

Share this post


Link to post
Share on other sites

think this fixes it for 3.1.1.0 :

$file = FileOpen("TestErrors.txt", 2)
For $i = 1 To 999
    If $i <> Int($i/100 * 100 + .0000000000001) Then
        $s_err = $i & @TAB & $i/100*100 & @TAB & Int($i/100 * 100)
        FileWriteLine($file, $s_err)
    EndIf
Next
FileClose($file)
Exit
This will only fix it under the following conditions:
  • The numbers are positive. If you need to adapt the solution to work with negative numbers, you will need to subtract the magic number JdeB provided.
  • There are very few consecutive floating point operations being performed on a number before it is "corrected". If you attempt to chain together several operations involving floating point numbers, the deviation from the "true" value may exceed the hard-coded threshold JdeB is providing.

Share this post


Link to post
Share on other sites

This will only fix it under the following conditions:

  • The numbers are positive. If you need to adapt the solution to work with negative numbers, you will need to subtract the magic number JdeB provided.
  • There are very few consecutive floating point operations being performed on a number before it is "corrected". If you attempt to chain together several operations involving floating point numbers, the deviation from the "true" value may exceed the hard-coded threshold JdeB is providing.

Thanks guys.

it seems to eliminate the error for writing into the file but actual calculation error is still in result.

I put this small script just to check if I had a problem.

In actual script value is read from input field that users fill in and function needs to convert the value from dollars.cents into cents for next step calculation - so conversion is $value = Int($value*100).

So how do I correct the actual error in calculations?

If it something cumbersome - don't bother wit it.

I used Int($value*100) to avoid checking number of digits after decimal point and stripping decimal point.

I will just add theses steps in.

Share this post


Link to post
Share on other sites

Thanks guys.

it seems to eliminate the error for writing into the file but actual calculation error is still in result.

I put this small script just to check if I had a problem.

In actual script value is read from input field that users fill in and function needs to convert the value from dollars.cents into cents for next step calculation - so conversion is $value = Int($value*100).

So how do I correct the actual error in calculations?

If it something cumbersome - don't bother wit it.

I used Int($value*100) to avoid checking number of digits after decimal point and stripping decimal point.

I will just add theses steps in.

Just add .0000000000001 within any INT() function to correct the calculation... (As long as the values are positive like Valik stated)...

Did you try that ?


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Just add .0000000000001 within any INT() function to correct the calculation... (As long as the values are positive like Valik stated)...

Did you try that ?

You right - it solves the problem. Thank you very much.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0