I am doing project over and the following is to check if the number is cube:
isInt x = x = from the integer (round x) isCube x = is $ x ** (1/3)
but due to a floating point error, it returns false results Is:
* main & gt; IsCube (384 ^ 3) false
Is there a way to implement a more reliable cube test?
On one side-note, this is my other solution, which is the filter (isCube) (perms n)
:
cubes = [n ^ 3 N & lt; - [code] does not work due to a type of interface error [1.]] Perms n = map $ permutations $ show n :: [integer] answer = head [n | N & lt; -cubes, (length $ filter (isCube) (perms n)) == 5] What should I do to fix the error?
No examples for the floating integer (RealFRC integer) arising from 'iscube' on prob62. HS: 10: 44-49
Any customization is also welcome; -)
Try using the floating point number as much as possible, especially if you have There is a problem that is related to integer values Floating point numbers have problems with the sphere and certain values (like 1/3) can not be perfectly represented, so do not you wonder if you get mysterious answers .
First of all, in order to correct your typing error, you have to define isCube
again. If you check this type of signature then it looks like:
isCube :: (RealFrac a, Floating A) => A - & gt; Boole
Note that it looks like something that is the first argument of class floating
. Your problem is that you use this function on integer values And do not have an example of integers floating
. You can redefine the isCube
to check the function type.
isCube x = isInt $ (interagral x) ** (1/3)
However, this will not be true of your program.
One way to make your program more correct is to suggest Heinrich. It will look like this:
isCube x = (round (from integral x ** (1/3)) ^ 3 == x
Good luck!
No comments:
Post a Comment