Jump to content

Little Math bug?


Go to solution Solved by JohnOne,

Recommended Posts

Hello everyone,

I have a little logical question about basic math with AutoIT. I did caunting a little bit and somehow my program giveing me back wrong number all the time. I start searching for the problem and run step by step till i found out this:

Local $Number = 19.2

If $Number > 0 Then
Do
  $Number = $Number - 1
Until $Number < 1
EndIf
MsgBox(0, "", $Number)

This should give me back 0.2 but somehow it giveing as resoult 0.199999999999999 and it is not accurate. Any idea how it is posible? It is totaly unlogic for me. I do not know why it is not accurate, but if i write only this: 1.2 - 1 It give the right resoult (0.2). If it is some kinda bug, then can anyone help to me, how i can get the 0.2 out from $number? Cause i need only the decimal number. I use the integer part for other calculation.

$Number is a dynmic number, but in example i use a fix number cause it is easyer.

Sry for my bad English, and double sry, but I am learning AutoIT language by myself. :)

[u]Tricky[/u]

You can't teach a man anything, you can only help him, find it within himself. (Galileo Galilei)

Link to comment
Share on other sites

  • Solution

Something to do with floating point math.

If you are just wondering why it does not work, I cannot help you.

If you're trying to fix a problem...

MsgBox(0, "", Round($Number, 1))

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

It's not a bug.

That behavior is normal when you do calculations with floating point representations.

An example: Can you write the number 1/3 in the decimal number system with a finite number of decimal digits?

No - it is something like 0.33333333333333333...

Exactly the same happens in the binary number system.

For example the number 0.2 can not represented exactly in that system.

It would have a endless number of decimal digits. But because we only have a endless number of digits this leads to an small accuracy error:

MsgBox(0, "", StringFormat("%.20f", 0.2))

So if you do calculations with these numbers the error proceeds and leads to your result.

It is not a bug - you reproduce this behavior in every programming language witch deals with floating point numbers.

Edited by AspirinJunkie
Link to comment
Share on other sites

JohnOne,

The problem reveal when you try to express values in decreasing powers of 2, since FP is binary-based.

0.2 = 1/8 + 1/16 + 1/128 + 1/256 + 1/2048 + 1/4096 + 1/32768 + 1/65536 + ...

That is:

0.2 = 2-3 + 2-4 + 2-7 + 2-8 + 2-11 + 2-12 + 2-15 + 2-16 + 2-19 + 2-20 + 2-23 + 2-24 ...

Hence you can see the binary pattern for this particular value:

0.00110011001100110011.........

This pattern is infinite so when stored in a floating-point register or variable, the value is forcibly truncated.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Thank you for the answares, i bet it will help.

//to JohnOne - To be honest, as you could read my signatures:

I am learning AutoIT language by myself.

 

I guess it explane a lot. :)

Have not worked with floating numbers yet in programing, so it is new for me. ;)

Sry for my bad English, and double sry, but I am learning AutoIT language by myself. :)

[u]Tricky[/u]

You can't teach a man anything, you can only help him, find it within himself. (Galileo Galilei)

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...