Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Turing "mod" fail
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Dreadnought




PostPosted: Sat May 05, 2012 1:15 pm   Post subject: Re: Turing "mod" fail

So you want to test for perfect cubes. You can take the cube root of a number and get a real number.

You are trying to check if the real value is an integer, however, real numbers are, at times, imperfect so this only works with a few cubes (1, 27 and 216 for numbers between 1 and 1000 it seems).

The mod operator can't really do much better. You know how to round that real number to an integer. Try to think of some way to use the rounded cube root to check if the original number was indeed a perfect cube.
Sponsor
Sponsor
Sponsor
sponsor
Aange10




PostPosted: Sat May 05, 2012 3:55 pm   Post subject: RE:Turing "mod" fail

Do mods in all languages do that?
mirhagk




PostPosted: Sat May 05, 2012 5:20 pm   Post subject: RE:Turing "mod" fail

depends on the langauge. In some cases mod only works on integers, in other cases it just does weird things with real numbers.
Aange10




PostPosted: Sat May 05, 2012 8:48 pm   Post subject: RE:Turing "mod" fail

That's actually very interesting. I wonder why that is... could you elaborate?
Dreadnought




PostPosted: Sat May 05, 2012 11:27 pm   Post subject: Re: Turing "mod" fail

Well, modular arithmetic is only defined for integers so how you extend the concepts into real numbers is not clear. For example, say you had a theorem in modular arithmetic that requires a number to be prime. How do you extend this to real numbers?
mirhagk




PostPosted: Sun May 06, 2012 8:37 am   Post subject: RE:Turing "mod" fail

the way the computer handles mod for integers is actually the exact same way they handle division (it's actually calculated at the same time). Floating point numbers are handled completely different, and are not precise.

Basically if your program relies on modular arithmetic for real numbers, change your program.
Aange10




PostPosted: Sun May 06, 2012 10:03 am   Post subject: RE:Turing "mod" fail

Interesting, thank you both. What about 125 ** 1/3 not= 5?
evildaddy911




PostPosted: Sun May 06, 2012 10:17 am   Post subject: RE:Turing "mod" fail

because 5 is an integer, whereas 125 ** 1/3 is a real number, 125 ** 1/3 will never equal "5"
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Sun May 06, 2012 2:54 pm   Post subject: Re: Turing "mod" fail

evildaddy911 wrote:

because 5 is an integer, whereas 125 ** 1/3 is a real number, 125 ** 1/3 will never equal "5"

Although that does seem to be a valid explanation, I'm afraid it is not.

Notice this
Turing:
for i: 1..10
    put i, "  ", (i ** 3) ** (1/3) = i
end for

% The result is
1  true
2  false
3  true
4  false
5  false
6  true
7  false
8  false
9  false
10  false

Notice that the cube roots of 1, 27and 216 are actually equal to 1, 3 and 6 even though the roots are real and 1, 3 and 6 are not.

The true reason is that real numbers are not perfectly accurate (they only store so many digits). The Wikipedia page has a nice example for 1/3.

Try this
Turing:
var num : real := 125 ** (1 / 3)
put frealstr(num, 16,15)

var num2 : real :=216 ** (1 / 3)
put frealstr(num2, 16,15)

% Result
5.00000000000000088817841970012523233890533447265625
6.00000000000000000000000000000000000000000000000000

Basically, 216 ** (1/3) (don't forget that 1/3 is a real too) happens to come out as 6 exactly once it is rounded (usually real numbers have 16 digits of accuracy). But this is not so for 125.

Hope this clear thing up.
Aange10




PostPosted: Sun May 06, 2012 5:00 pm   Post subject: RE:Turing "mod" fail

I knew that evildaddy, i didn't mean the typespecs to be equal, i meant the value.

@Dread, so the value of 125 ** 1/3 is 5.00000000000000088817841970012523233890533447265625, not because of exponential function, but because of the inaccuracy of 1/3 (.3repeating)?
Tony




PostPosted: Mon May 07, 2012 1:01 am   Post subject: Re: RE:Turing "mod" fail

Aange10 @ Sun May 06, 2012 5:00 pm wrote:
@Dread, so the value of 125 ** 1/3 is 5.00000000000000088817841970012523233890533447265625, not because of exponential function, but because of the inaccuracy of 1/3 (.3repeating)?

Kind of, but probably not for the reasons that you are thinking of. See http://en.wikipedia.org/wiki/IEEE_754 for the standard in how floating-point numbers are actually represented:
Quote:

(-1)^s * c * b^q

Where the base b is typically 2 (binary representation of the number). As such, there is simply no exact mapping between some decimal fractions and binary encodings. For what integer values of c and q would
code:

0.1 = c * 2^q

be true?

So something like 1/10 is just as problematic as 1/3.

A way to deal with this is to never compare numbers directly, but only with less-than and greater-than checks, within deltas. In the quoted example, 125 ** 1/3 is less than 0.000000000000001 away from 5.0. If that is acceptable for your calculations, you can just call it 5.0. Some languages have that build in.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Amarylis




PostPosted: Thu May 10, 2012 5:30 am   Post subject: RE:Turing "mod" fail

So, would doing something like...

[syntax="turing]if num > 5 and num < 5.000001 then
num := 5
end if[/syntax]


Solve the problem for 125 ** (1/3)? (What would be a better solution for this though?)
Dreadnought




PostPosted: Thu May 10, 2012 7:45 am   Post subject: Re: Turing "mod" fail

Dreadnought wrote:
You know how to round that real number to an integer. Try to think of some way to use the rounded cube root to check if the original number was indeed a perfect cube.

Here I was hinting at a simple way of doing things. Say I give you 28, you compute 28 ** (1/3) and round it to an integer, this gives you 3. How can you easily figure out if 3 is the cube root of 28? (which will tell you if 28 is a perfect cube)
evildaddy911




PostPosted: Thu May 10, 2012 3:36 pm   Post subject: RE:Turing "mod" fail

if a ** b = c then
c ** (1 / b) = a
end if
Dreadnought




PostPosted: Thu May 10, 2012 4:02 pm   Post subject: Re: Turing "mod" fail

evildaddy911 wrote:

if a ** b = c then
c ** (1 / b) = a
end if

Except when b = 0. Razz
But that's the idea.

This gives an easy check for a perfect cube
Turing:
if (round (a ** (1/3)) ** 3) = a then
   put "a is a perfect cube"
end if
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 30 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: