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

[JASS] From real to integer

Status
Not open for further replies.

Ardenian

A

Ardenian

How does the conversion from real to integer behaves ?

Does it like in math, 3 -> 3.4999999 becomes 3 and 3.50 -> 3.9999999 becomes 4 ?

Can I directly compare the real and the integer, like:

JASS:
if real r > integer i then

endif

Or do I have to convert the integer back to real first ? ( the answer is obvious, isn't it, I have to convert it, don't I ?)
 
Reals are truncated when converted into integers - this means that the decimals are simply stripped off. If you want to round the number, you can use the following function:

JASS:
function Round takes real r returns integer
    return R2I(r+0.5) 
endfunction

I know that the compiler will sometimes complain about comparing integers with reals, so you should probably convert them.
 

Ardenian

A

Ardenian

Thanks for your answer!

Hm, my problem is, I get two reals with around three positions, like 3,142 or 3,890
and I need exactly this range. After rounding it should still be in the range between 3 and 4, in the previous example.

Using the function would not allow this, would it ?
Adding 0.5 to the real value will make every real under 3.5 become greater than 3.5, but as you say, it is simply cut off and as every value above 3.5 will make it be greater than 4...
Okay, never mind.

Thank you!
 
Status
Not open for further replies.
Top