Detecting if Real is an Integer

Tawnttoo

New Member
Reaction score
35
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,456
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
35
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
Staff member
Reaction score
960
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,456
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
35
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,456
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
35
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
35
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.
  • jonas jonas:
    Is the school system in your country similar to Chinese school system? I could never imagine being a teacher in a Chinese school, what's expected of students and teachers is just so different from what I'd be looking for as a teacher
  • vypur85 vypur85:
    A Levels is based on the UK syllabus offered internationally. So the syllabus is similar throughout different countries, taught in english.
  • vypur85 vypur85:
    I can't speak or write or read much chinese... Too difficult....... Which also makes my life here in china a lil difficult.
  • jonas jonas:
    Oh, I see. I thought Chinese version of A-levels :D
  • jonas jonas:
    I've been using the translate app a lot on my phone. take a photo, translate. take another photo, translate again :D
  • jonas jonas:
    it also has voice translation, my colleagues sometimes use that
  • jonas jonas:
    You came during the 0 covid policy right? How'd you get through quarantine? Did you have some help with the apps and green codes?
  • tom_mai78101 tom_mai78101:
    If you have any Chinese you need help with, let me know.
    +1
  • vypur85 vypur85:
    @jonas Those were the things I did too. Translate app and VPN are always the most important things to have to survive. Lol.
  • vypur85 vypur85:
    Yea I came last year. I was quarantined for about 30 days. Fml. The first day of my quarantine was the eve of Chinese new year last year. Fml again... Lol.
  • vypur85 vypur85:
    @tom_mai78101 IIRC you're from Taiwan right.
  • The Helper The Helper:
    I thought China had cracked down on having any foreign teachers?
  • vypur85 vypur85:
    Yeah I've heard of the news before. My school is still actively hiring foreign teachers. Not sure how things work now.
  • The Helper The Helper:
    yeah it is hard to get reliable information about that kind of stuff
  • The Helper The Helper:
    but you are doing it so it is still doable
  • vypur85 vypur85:
    I think it doesn't affect existing foreign teachers. Then again, its still weird that my school is hiring.
  • The Helper The Helper:
    Happy Monday people hope everyone has a fantastic week!
  • Ghan Ghan:
    Happy Monday?! That's a contradiction.
  • jonas jonas:
  • jonas jonas:
    please put this in the news section, it's crazy
  • tom_mai78101 tom_mai78101:
    Technically, you can post in the Headline News. Just posted your news there.
  • tom_mai78101 tom_mai78101:
    I'm not aware of you not being able to post news.
  • jonas jonas:
    I think technically I can I just forget how
    +1
  • The Helper The Helper:
    Happy Thursday!

    The Helper Discord

    Members online

    No members online now.

    Affiliates

    Hive Workshop NUON Dome World Editor Tutorials

    Network Sponsors

    Apex Steel Pipe - Buys and sells Steel Pipe.
    Top