Detecting if Real is an Integer

Tawnttoo

New Member
Reaction score
36
The title may be misleading but what I need is a way to check if a real is in this form: X.00 as in a whole number?
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
if (real to int( REAL ) ) is equal to REAL

real to integer always rounds down. no matter what values follow the floating point. 7.1 and 7.99 both are 7 if converted into an integer.
 

Tawnttoo

New Member
Reaction score
36
But if I'm using a 0.05 second interval and reduce a real bu 0.05 each time, how can I check if it is a whole number?
 

mylemonblue

You can change this now in User CP.
Reaction score
7
I think so. Because what your method would do is once the .05 makes, say, 6 fall below 6.00 to 5.95 it would automatically reduce it to 5.00 which isn't what he wants. What I see is he wants it to tell him once the number becomes a whole number like so:

Each line is after every x seconds.
6.00 true
5.50 false
5.40 false
5.30 false
...
...
5.10 false
5.00 true
4.90 false

and so on.
 

muzk

Member
Reaction score
3
Just transform into a integer your real and then sustract, if it is equal to 0 then your number is a integer, if not, its a real. Example:
5.0 is your real
5 is the integer of that real
5.0-5 = 0
So your real is an integer.

Other
5.5 is your real
5 is the integer part of that real
5.5-5 = 0.5 , so your real isnt a integer.
 

drol

New Member
Reaction score
7
Just transform into a integer your real and then sustract, if it is equal to 0 then your number is a integer, if not, its a real. Example:
5.0 is your real
5 is the integer of that real
5.0-5 = 0
So your real is an integer.

Other
5.5 is your real
5 is the integer part of that real
5.5-5 = 0.5 , so your real isnt a integer.

Just as a hint: to do this in GUI you should use "Convert Real to Integer".

The check will be something like this:
Code:
YOURNUMBER - (convertToReal (convertToInt ( YOURNUMBER ) )) == 0
 

Romek

Super Moderator
Reaction score
963
There's no need to subtract. Just compare the value to the original real, rather than subtracting and comparing to 0.
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
I think so. Because what your method would do is once the .05 makes, say, 6 fall below 6.00 to 5.95 it would automatically reduce it to 5.00 which isn't what he wants. What I see is he wants it to tell him once the number becomes a whole number like so:

Each line is after every x seconds.
6.00 true
5.50 false
5.40 false
5.30 false
...
...
5.10 false
5.00 true
4.90 false

and so on.

maybe i should give some examples to clarify:

lets look at this:
if (real to int( REAL ) ) is equal to REAL

lets say our value REAL is 6.00
we check
if (real to int(6.00) == 6.00) => if (6 == 6.00) => true

we substract 0.25 and check:
if (real to int (5.75) == 5.75) => if (5 == 5.75) => false

we substract 0.25 and check:
if (real to int (5.5) == 5.5) => if (5 == 5.5) => false

we substract 0.25 and check:
if (real to int (5.25) == 5.25) => if (5 == 5.25) => false

we substract 0.25 and check:
if (real to int (5.0) == 5.0) => if (5 == 5.00) => true

we substract 0.25 and check:
if (real to int (4.75) == 4.75) => if (4 == 4.75) => false

... and so on and so on...

that means we will get TRUE whenever REAL is a whole number like 1.00, 2.00, 3.00, 4.00, and so on.
We will get FALSE whenever there are other digits then 0 after the floating point. for example 5.47 because (5 != 5.47)
 

Tawnttoo

New Member
Reaction score
36
So, if I'm using the 0.05-second-interval trigger for 20 seconds, do I have to check if its 20.00, 19.00, 18.00 etc in conditions? Using 'Or-multiple conditions'?
 

Accname

2D-Graphics enthusiast
Reaction score
1,462
no, you just do the following:
(written in pseudo code)

Code:
MyReal = 20

loop do
  MyReal -= 0.05
  if (MyReal.to_int) == MyReal
     comment: ITS AN INTEGER! HURRAY!
  else
     comment: ITS NOT AN INTEGER.....
  end
end
 

Tawnttoo

New Member
Reaction score
36
I have this:

Trigger:
  • Game - Display to (All players) the text: ((String((Load (Key duration) of (Key (Picked unit)) from Surge_Hash))) + ( not equal to + (String((Integer((Load (Key duration) of (Key (Picked unit)) from Rune_Hash)))))))


It always shows the integer as 0.
 

muzk

Member
Reaction score
3
JASS:
        if RqI(6.0) then
            call BJDebugMsg("true!")
        else
            call BJDebugMsg("false!")
        endif

Obiusly it shows false cuz the result is 0 == 0.0

Other thing,
Logicaly, A - R2I(A) == 0.0 is the same as A == R2I(A)
 

DioD

New Member
Reaction score
57
equal comparison will fail, it will count 6.001 and 6.002 as same reals, but this is not true.

with not equal it will detect fine.
 

muzk

Member
Reaction score
3
did you tested?
anyways we are comparing with the integer part I dont see your point :(
 

Tawnttoo

New Member
Reaction score
36
For some reason, it sets it to an integer below when it hits a whole number. E.g. 18.00 ---> 17

EDIT: got it to work by adding 1 to the integer. Thanks for the help!
 

Bribe

vJass errors are legion
Reaction score
67
The [LJASS]ModuloReal[/LJASS] function may interest you. See also [LJASS]ModuloInteger[/LJASS]. If I wanted to see if an integer was a multiple of 10...

JASS:
if ModuloInteger(i, 10) == 0 then
    // i is divisible by 10... some examples of i would be -50, 60, or even 0.
endif
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top