Jump to content

Is this a bug in INT() ?


is8591
 Share

Recommended Posts

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
Link to comment
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.
Link to comment
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?

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

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

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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.
Link to comment
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.

Link to comment
Share on other sites

  • Developers

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 ?

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...