• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!

Wrong result after Real to Int Conversion

Status
Not open for further replies.
Level 3
Joined
Aug 26, 2019
Messages
16
Hello everybody,

i encountered some strange behavior today. Maybe somone can help me. In a Trigger i simply convert a Real variable to an Int. so for example 2.00 (Real) to 2 (Int). But after 3 the conversion goes wrong? The editor then says that 4.00(Real) is 3(Int). Somone knows what the problem is? Trigger and log in screenshots.
 

Attachments

  • Log.png
    Log.png
    6.4 MB · Views: 22
  • Triggers.png
    Triggers.png
    39.7 KB · Views: 22
It's a classic one. All floating-point number (reals for example, but also in programming in general) are represented using a format that can't represent all numbers exactly in order to save memory, but get very close to it.
When you convert a real to integer, for example 4.9, you "cut" away everything after the ".", resulting in 4.

Combining these 2 facts a 4.00 isn't 4.00, it's actually (somewhere around) 3.9999999999, resulting in 3.
This is the case for a lot of "reals".

Therefore, in order to get the "correct integer number", add 0.01 or something and it'll be fine.
 
Status
Not open for further replies.
Top