Computer Science Canada command to check integer? |
Author: | BenLi [ 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? |
Author: | NikG [ Tue Aug 01, 2006 6:03 pm ] | ||
Post subject: | |||
Couldn't find a function... I believe this should work most (if not all) of the time:
|
Author: | MysticVegeta [ Tue Aug 01, 2006 6:43 pm ] | ||
Post subject: | |||
what if the real # was pi? round(3.14) = 3 3.14 not= 3. Error there in the round fucntion. How about this?
|
Author: | Windsurfer [ Tue Aug 01, 2006 7:15 pm ] | ||||
Post 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?
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?
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). |
Author: | NikG [ Tue Aug 01, 2006 9:04 pm ] | ||
Post subject: | |||
MysticVegeta wrote: what if the real # was pi? round(3.14) = 3
MysticVegeta, as Windsurfer has already shown for me, that's the point... 3.1 not= 3 so you know it's not an integer.
3.14 not= 3. Error there in the round fucntion. MysticVegeta wrote: How about this?
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.
|
Author: | Cervantes [ Tue Aug 01, 2006 9:34 pm ] | ||||||
Post 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
it would be
This does work properly. Another method is to use mod.
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. |
Author: | BenLi [ Wed Aug 02, 2006 11:02 am ] |
Post subject: | |
weird, i definitely remember such a command somwhere in these forums, can't find it though |
Author: | MysticVegeta [ Wed Aug 02, 2006 1:03 pm ] |
Post subject: | |
NikG wrote: MysticVegeta wrote: what if the real # was pi? round(3.14) = 3
MysticVegeta, as Windsurfer has already shown for me, that's the point... 3.1 not= 3 so you know it's not an integer.
3.14 not= 3. Error there in the round fucntion. 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. |
Author: | Mazer [ Wed Aug 02, 2006 1:42 pm ] |
Post subject: | |
What? |
Author: | Albrecd [ Wed Aug 02, 2006 2:15 pm ] |
Post 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? |
Author: | richcash [ Wed Aug 02, 2006 2:40 pm ] | ||
Post 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 :
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. |
Author: | [Gandalf] [ Wed Aug 02, 2006 3:51 pm ] | ||
Post subject: | |||
richcash wrote: I have no clue what anyone else is saying.
We are saying:
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! ![]() |
Author: | Cervantes [ Wed Aug 02, 2006 4:42 pm ] | ||
Post 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 :
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. |
Author: | Clayton [ Wed Aug 02, 2006 8:34 pm ] | ||
Post 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... |
Author: | NikG [ Wed Aug 02, 2006 10:23 pm ] |
Post 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. |
Author: | McKenzie [ Thu Aug 03, 2006 9:10 am ] |
Post subject: | |
Cervantes wrote: Might there be some way to check using bitwise operators? I'm not sure how Turing stores its real numbers.
I'm fairly certain Turing is not the one deciding on the representation of real. Regardless, I would assume they use standard IEEE 64 bit floating point. This would be 1 sign bit, 11 exponent bits and 52 bits for the mantisa (I can explain if you care.) There are two major problems with doing a bit check to see if there is no fractional component of the number. 1. All of turing's bitwise opperators only work on integers. This means you would need to do some type cheating for this to work (e.g. get a real pointer and use cheat to make it an int pointer or dump the real to a binary file and read it back as 2 integers) 2. The real format does not lend itself to a bitwise comparison for this. The number is not seperated into whole and fractional components. It is essentially a base 2 scientific notation. So something as simple as 3 looks like 1.1 * 2^1. |
Author: | Windsurfer [ Thu Aug 03, 2006 1:03 pm ] |
Post subject: | |
I think that, because we aparently are all acheiving the same thing, but there seems to be confusion on which one is "better", some benchmarking is in order. The time (in miliseconds) for 1'000'000 iterations of each on my machine: round (num) = num 1121 num mod 1 = 0 1018 num - round (num) = 0 1379 So, mod appears to be the winner. |
Author: | [Gandalf] [ Thu Aug 03, 2006 1:26 pm ] |
Post subject: | |
Windsurfer wrote: So, mod appears to be the winner.
It's faster because it's doing less operations compared to the others. As for you're declaration that because it is fastest it is best, I would disagree. Especially in this case, I doubt speed means much to the programmer. The clarity of the code would be a more important factor in deciding which method to use. |
Author: | Windsurfer [ Thu Aug 03, 2006 1:32 pm ] |
Post subject: | |
So, even if one function is at least 10% faster, you would still chose organisation and clarity over efficiency? For some reason, different code for sake of clarity makes me iffy. I'm always for optimisation... It's how i managed to have 2 frame buffers on Forces ![]() |
Author: | richcash [ Thu Aug 03, 2006 1:37 pm ] | ||
Post subject: | |||
Well, one other reason the mod way is good is if you wanted to go further and find out the decimal remainder :
Yes, I'm pretty sure Turing uses 64 bit floating point representation (8-byte) for regular real values, and uses 32 bit floating point representation for real4 type values (4-byte). That's why real can also be written as real8 (the last number - 4 or 8 - represents the floating point in bytes.) In case you're wondering, real4 is very inaccurate after 4 decimal places but it is faster with calculations, while real or real8 is accurate to 16 decimal places but slower with calculations. @ Mr. McKenzie (#2) - wow, that's really interesting! I thought it would be in binary, but I guess base 2 scientific notation makes more sense. One way I thought of that might be 100% accurate is to convert the real to a string and search the string for a ".", and if the new string value has a decimal, you know the number wasn't an integer. |
Author: | NikG [ Thu Aug 03, 2006 2:18 pm ] | ||
Post subject: | |||
richcash wrote: One way I thought of that might be 100% accurate is to convert the real to a string and search the string for a ".", and if the new string value has a decimal, you know the number wasn't an integer.
|
Author: | McKenzie [ Thu Aug 03, 2006 2:33 pm ] |
Post subject: | |
richcash wrote: I thought it would be in binary, but I guess base 2 scientific notation makes more sense.
Well, it still is in binary. It's just in a sort of scientific notation. Take a number like 20. In binary you have: 10100 As a real you would shift it right until all you have left of the decimal is a 1. What's to the right is your mantisa. The number of shifts is your exponent. So 20 as a real would look like 0 00000000100 0100000000000000000000000000000000000000000000000000 <spaces added for readability> |
Author: | richcash [ Thu Aug 03, 2006 4:20 pm ] | ||
Post subject: | |||
@NikG - Oh my gosh, my idea is horrible. I didn't test it, I had more faith in the realstr function. I thought any real value can get accurately converted into a string.
This ouputs 1, oh well! |
Author: | MysticVegeta [ Thu Aug 03, 2006 6:21 pm ] |
Post subject: | |
Albrecd wrote: [quote="MysticVegeta]what if the real # was pi? round(3.14) = 3
3.14 not= 3. Error there in the round fucntion. 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?[/quote] Well I was trying to support the solution cervantes proposed, but I made it more confusing. Sorry about that. You see what I was trying to tel was that someone put the check as "round (3.1) ?= 3" but the check should be "round (3.1) ?= 3.1". Thats what cervantes said, I was just trying to clarify that but I made it more confusing... sorry ![]() |
Author: | NikG [ Thu Aug 03, 2006 7:02 pm ] |
Post subject: | |
MysticVegeta wrote: You see what I was trying to tel was that someone put the check as "round (3.1) ?= 3" but the check should be "round (3.1) ?= 3.1". Thats what cervantes said, I was just trying to clarify that but I made it more confusing... sorry No one said it should be round(3.1)=3. Go look at my original solution, I said round(var)=var, where var is the number you're trying to test.
![]() And Cervantes brought the mod solution to focus, not the round one. |
Author: | MysticVegeta [ Fri Aug 04, 2006 10:42 am ] |
Post subject: | |
Hmm wow you are right. I thought I saw that somewhere before on the post.. weird. O_o. now that just makes me look and feel like an idiot. ![]() |