Author |
Message |
difi13
|
Posted: Fri May 04, 2012 3:01 pm Post subject: Turing "mod" fail |
|
|
Hello Guys and Girlz.
I want to share the magic of turing with you.
ok so lets start with taking a cube root of something for example
Turing: |
put 64 ** (1/3) %will output 4
|
Turing: |
put 4 mod 1 %will output 0
|
and finally
Turing: |
put (64 ** (1/3)) mod 1 %will output 1 !!!!!!!!!
|
I would love to know why this happens and possibly the solution
Please specify what version of Turing you are using
last one. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
Tony

|
Posted: Fri May 04, 2012 3:10 pm Post subject: RE:Turing "mod" fail |
|
|
64 ** (1/3) likely results in a real (floating point) type, not int. What do you get when you run
code: |
put round((64 ** (1/3))) mod 1
|
|
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
difi13
|
Posted: Fri May 04, 2012 3:55 pm Post subject: RE:Turing "mod" fail |
|
|
its 0 , thanks.
but there is one more problem if make a for loop.
for x : 1..1000
if x ** (1/3) mod 1 = 0 then
put "Cool"
end if
it will never print cool
but if we go
for x : 1..1000
if round(x ** (1/3)) mod 1 = 0 then
put "Cool"
end if
Cool will print 1000 times.
So the question is how to make it print something if a cube root of a number is an integer(has no decimals) |
|
|
|
|
 |
Tony

|
|
|
|
 |
difi13
|
Posted: Fri May 04, 2012 4:14 pm Post subject: RE:Turing "mod" fail |
|
|
It is obvious that it rounds the number so anything rounded mod 1 will equal to 0. Now the problem comes "how to make it print something if a cube root of a number is an integer(has no decimals)" |
|
|
|
|
 |
crossley7
|
Posted: Fri May 04, 2012 4:17 pm Post subject: RE:Turing "mod" fail |
|
|
think about the easiest way to check if a number is an integer or not. Refer to Tony's last post for a clue.
In fact, you answered your own question in the same post. Think about it a bit and you will see your answer.
Don't use mod for this type of thing since it really works best with integers only and not with reals. |
|
|
|
|
 |
difi13
|
Posted: Fri May 04, 2012 5:05 pm Post subject: RE:Turing "mod" fail |
|
|
I tried so much stuff. Even stupid check like this:
if 64 ** (1/3) = round (64 **(1/3)) then
put "Good"
end if
will never print Good. But both numbers are 4 |
|
|
|
|
 |
Tony

|
Posted: Fri May 04, 2012 5:21 pm Post subject: RE:Turing "mod" fail |
|
|
no, they are not both 4
The first number is 4.0
The second number is 4
ProTip, don't use = comparison on float-point numbers. 4.0 = 4.0 is not necessary true (you'd need to understand float-point standard to understand why that might be the case). So hint: you'd need to compare integers; but round(x) = round(x) obviously doesn't tell you anything useful, so there's some extra work to be done. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
Sponsor Sponsor

|
|
 |
difi13
|
Posted: Fri May 04, 2012 6:04 pm Post subject: RE:Turing "mod" fail |
|
|
I give up ... |
|
|
|
|
 |
Tony

|
Posted: Fri May 04, 2012 7:10 pm Post subject: RE:Turing "mod" fail |
|
|
lets try with a specific example: 4.2
is it an integer? How do you know? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
 |
difi13
|
Posted: Fri May 04, 2012 8:37 pm Post subject: RE:Turing "mod" fail |
|
|
it is not because there is a decimal. |
|
|
|
|
 |
Aange10

|
Posted: Sat May 05, 2012 10:13 am Post subject: RE:Turing "mod" fail |
|
|
And you learned that if there is a decimal, (such as 4.2 OR the output of 64 ** (1/3)) then when you mod it by 1 it is not= 0. Likewise you know that if it is an integer (has no decimals) and is modded by 1, then it comes out to be 0.
With the above logic, you can tell if a number is an integer (not decimals) or a real number (has decimals). Unless the real number ends with .0, such as 4.0. If that matters to you, I suggest using your numbers as strings and seeing if they contain a ".".
Also I wanted to point out that the round() function turns a real number into an integer (takes away the decimals). |
|
|
|
|
 |
difi13
|
Posted: Sat May 05, 2012 11:59 am Post subject: Re: RE:Turing "mod" fail |
|
|
Aange10 @ Sat May 05, 2012 10:13 am wrote:
Also I wanted to point out that the round() function turns a real number into an integer (takes away the decimals).
Heh .. It seems that turing disagrees with you on that.
round(64**(1/3)) still returns 4.0 and will never be = to 4. |
|
|
|
|
 |
Dreadnought
|
Posted: Sat May 05, 2012 12:28 pm Post subject: Re: Turing "mod" fail |
|
|
difi13 wrote:
Aange10 @ Sat May 05, 2012 10:13 am wrote:
Also I wanted to point out that the round() function turns a real number into an integer (takes away the decimals).
Heh .. It seems that turing disagrees with you on that.
round(64**(1/3)) still returns 4.0 and will never be = to 4.
Have you tried it?
Turing: | put round(64 ** (1/ 3)) = 4
>> true
put round(64 ** (1/ 3))
>> 4
% Using Turing 4.1.1 (last official release of Turing)
|
Please give suggestions a try before saying they don't work. |
|
|
|
|
 |
difi13
|
Posted: Sat May 05, 2012 12:45 pm Post subject: RE:Turing "mod" fail |
|
|
Sorry I meant that this would not work in my case since I need this sort of construction.
for x : 1..1000
if round(x ** (1/3)) mod 1 = 0 then
put "Cool"
end if
I am looking to find which numbers (cubed) will not give me any decimals.
If I round it, the outcome will be that all the numbers will have no decimals.
I hope that you understand what I am saying |
|
|
|
|
 |
|