why doesn't this work?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

why doesn't this work?

Alexander Chen
hi,

for a List comprehension I want to only include the integers of a division. In my beginners mind this should work:

(round (x / y)) == (x / y)

however, i get a nontrivial error blurp.

what am i doing wrong?

best,

Alexander 




_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: why doesn't this work?

Oliver Charles-3
On Wed, 8 Jan 2020, at 3:01 PM, Alexander Chen wrote:
hi,

for a List comprehension I want to only include the integers of a division. In my beginners mind this should work:

(round (x / y)) == (x / y)

however, i get a nontrivial error blurp.

Could you share what the "nontrivial error blurp" is? It will be easier for people to reply to you if they can see the problem without
having to reproduce it manually.

Ollie


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: why doesn't this work?

Ut Primum
In reply to this post by Alexander Chen
Hi,
I think the problem is in the types.
If you look at the type of the function round, you can see its result is an Integral:

>> :t round
round :: (Integral b, RealFrac a) => a -> b

The result of the division x/y, instead, is a Fractional, and you cannot test equality between two things of different types.
To solve the problem you can do a type cast, by converting the Integral to a Num, using fromInteger:

(fromInteger (round (x/y))) == (x/y)

This should work.

Cheers,
Ut




Il giorno mer 8 gen 2020 alle ore 16:01 Alexander Chen <[hidden email]> ha scritto:
hi,

for a List comprehension I want to only include the integers of a division. In my beginners mind this should work:

(round (x / y)) == (x / y)

however, i get a nontrivial error blurp.

what am i doing wrong?

best,

Alexander 



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: why doesn't this work?

David McBride
In reply to this post by Alexander Chen
Floating point comparisons are a bad idea.

A better way would be x `mod` y == 0.

On Wed, Jan 8, 2020 at 10:01 AM Alexander Chen <[hidden email]> wrote:
hi,

for a List comprehension I want to only include the integers of a division. In my beginners mind this should work:

(round (x / y)) == (x / y)

however, i get a nontrivial error blurp.

what am i doing wrong?

best,

Alexander 



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners