• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.
  • Create a faction for Warcraft 3 and enter Hive's 19th Techtree Contest: Co-Op Commanders! Click here to enter!
  • Create a void inspired texture for Warcraft 3 and enter Hive's 34th Texturing Contest: Void! Click here to enter!
  • The Hive's 21st Texturing Contest: Upgrade is now concluded, time to vote for your favourite set of icons! Click here to vote!

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: 25
  • Triggers.png
    Triggers.png
    39.7 KB · Views: 25
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