Author |
Message |
BenLi
|
Posted: Tue Aug 01, 2006 4:35 pm Post subject: command to check integer? |
|
|
does a function exist to tell whether a real number is an integer? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
NikG
|
Posted: Tue Aug 01, 2006 6:03 pm Post subject: (No subject) |
|
|
Couldn't find a function...
I believe this should work most (if not all) of the time: code: | if round(var) = var then
... |
|
|
|
|
|
|
MysticVegeta
|
Posted: Tue Aug 01, 2006 6:43 pm Post subject: (No subject) |
|
|
what if the real # was pi? round(3.14) = 3
3.14 not= 3. Error there in the round fucntion.
How about this?
code: | if (strintok (realstr (real, width) ) ) then
# is int
end if
|
|
|
|
|
|
|
Windsurfer
|
Posted: Tue Aug 01, 2006 7:15 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: what if the real # was pi? round(3.14) = 3
3.14 not= 3. Error there in the round fucntion.
How about this?
code: | if (strintok (realstr (real, width) ) ) then
# is int
end if
|
What? i don't understand your reasoning :S
Like, i'm not even sure if your idea works... and isn't it just more complicated?
code: |
const num := 3
if round (num) = num then
put "it's an integer!"
else
put "it's not an integer."
end if
|
That works perfectly (nearly... at 3.0000000000000001 it thinks it's an integer... that's due to Turing's floating point accuracy losing track of the last digit). |
|
|
|
|
|
NikG
|
Posted: Tue Aug 01, 2006 9:04 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: what if the real # was pi? round(3.14) = 3
3.14 not= 3. Error there in the round fucntion. MysticVegeta, as Windsurfer has already shown for me, that's the point... 3.1 not= 3 so you know it's not an integer.
MysticVegeta wrote: How about this?
code: | if (strintok (realstr (real, width) ) ) then
# is int
end if
|
I thought of using strintok too, but the problem lies with the realstr function. Think about it: you'd have to pass a real number with a certain width into realstr. What do you use as width? If you use 1, then the function will work only when the number being tested is indeed a real number. Try it and see. |
|
|
|
|
|
Cervantes
|
Posted: Tue Aug 01, 2006 9:34 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: what if the real # was pi? round(3.14) = 3
3.14 not= 3. Error there in the round fucntion.
The check wouldn't be
code: |
if round(3.14) = 3 then
|
it would be
code: |
if round (3.14) = 3.14 then
|
This does work properly.
Another method is to use mod.
code: |
if num mod 1 = 0 then
% num is an integer
|
I thought there was some better way. If it is, I think it's a very uncommon command. It might be worth your while looking through the depths of the Turing Manual at some of the stranger looking stuff. Stuff like 'cheat' and 'addr', but not those. |
|
|
|
|
|
BenLi
|
Posted: Wed Aug 02, 2006 11:02 am Post subject: (No subject) |
|
|
weird, i definitely remember such a command somwhere in these forums, can't find it though |
|
|
|
|
|
MysticVegeta
|
Posted: Wed Aug 02, 2006 1:03 pm Post subject: (No subject) |
|
|
NikG wrote: MysticVegeta wrote: what if the real # was pi? round(3.14) = 3
3.14 not= 3. Error there in the round fucntion. MysticVegeta, as Windsurfer has already shown for me, that's the point... 3.1 not= 3 so you know it's not an integer.
Yes but round (3.1) = 3. and 3.1 is not an integer. The check would be round (3.1) =? 3.1. which is not true since round (3.1) not= 3.1, it = 3. therefore its not an integer, just like how cervantes pointed it out. the check would be round (num) = num? if it is, then it is an int, else its real. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Mazer
|
Posted: Wed Aug 02, 2006 1:42 pm Post subject: (No subject) |
|
|
What? |
|
|
|
|
|
Albrecd
|
Posted: Wed Aug 02, 2006 2:15 pm Post subject: (No subject) |
|
|
[quote="MysticVegeta]what if the real # was pi? round(3.14) = 3
3.14 not= 3. Error there in the round fucntion.[/quote]
But pi is not an integer...
I'm really confused as to whay you're trying to say, MysticVegeta. You seem to be disagreeing with the offered solution, and then using identical solutions to try to "fix" it. Could you clarify? |
|
|
|
|
|
richcash
|
Posted: Wed Aug 02, 2006 2:40 pm Post subject: (No subject) |
|
|
Ok, I get what Cervantes is saying with the mod, that's the best way, but I have no clue what anyone else is saying. An extremely obvious way to do it would be :
code: | if num - round (num) = 0 then
%number is an integer, do whatever
end if |
where num is what you're checking to be an integer or not. This may not be the best method (Cervantes') but it's easy to visualize. |
|
|
|
|
|
[Gandalf]
|
Posted: Wed Aug 02, 2006 3:51 pm Post subject: (No subject) |
|
|
richcash wrote: I have no clue what anyone else is saying.
We are saying:
code: | if round (num) = num then
%num is an integer
else
%num is a real number
end if |
Which translates to: if a number (3.5) is equal to that number rounded to the nearest whole number (4) it is an integer (3.5 is not an integer). Try it out with a number that is an integer: 6 is equal to 6 rounded (6), so it's an integer.
Hope I've not made things worse! |
|
|
|
|
|
Cervantes
|
Posted: Wed Aug 02, 2006 4:42 pm Post subject: (No subject) |
|
|
richcash wrote: Ok, I get what Cervantes is saying with the mod, that's the best way, but I have no clue what anyone else is saying. An extremely obvious way to do it would be :
code: | if num - round (num) = 0 then
%number is an integer, do whatever
end if |
where num is what you're checking to be an integer or not. This may not be the best method (Cervantes') but it's easy to visualize.
You're code is identical, mathematically, to what other people are saying.
I'll start with your equation and work towards the other equation.
num - round(num) = 0
# Add round(num) to both sides
num - round(num) + round(num) = 0 + round(num)
num = round(num)
Might there be some way to check using bitwise operators? I'm not sure how Turing stores its real numbers. |
|
|
|
|
|
Clayton
|
Posted: Wed Aug 02, 2006 8:34 pm Post subject: (No subject) |
|
|
i think it stores its real numbers in a constant size, however there is a function in Turing that finds the space used by a variable, however i dont know what function it is offhand, search through the F10 file for it
an example of it would be this:
[sytnax="Turing"]
var num : int1 := 2
var numSize : int := functionName (num)
[/syntax]
look around and see what you can find, it might help out, but the
is probably your best bet... |
|
|
|
|
|
NikG
|
Posted: Wed Aug 02, 2006 10:23 pm Post subject: (No subject) |
|
|
MysticVegeta, do you get that your last post was exactly the point of the solution I offered?
richcash, your solution is the same thing as mine! But I too am now leaning towards Cervantes' usage of mod. |
|
|
|
|
|
|