tags:

views:

230

answers:

3

I would like to do smth like:

x `mod` 1.0 == 0 // => int

but it seems mod works only for int... help! EDIT: I am trying to check if given number is triangle, http://en.wikipedia.org/wiki/Triangle_number so my idea was to check if n1 is Int...

(n*(n+1))/2 = s => n1 = (-1 +sqrt(1 + 8s))/2

A: 

Total edit:

Okay, I'm still not sure what you're trying to accomplish here.

  • First, anything modulo 1 is going to be zero, because the modulo function only makes sense on integers. If you want to take the modulo of a fractional type you can convert to an integer first. Edit: Although for what it's worth, Data.Fixed does have a mod' function for non-integral values.
  • I also don't know what you mean by "check if n1 is Int". Either it is or it isn't; you don't need to check at run time. Edit: Okay, I see now that you're just checking to see if a value has a fractional component. Paul Johnson correctly points out above that it's wise to be careful doing such things with floating point values.
  • If you want to mix mod and sqrt operations in the same calculation, you'll have to manually convert between appropriate types. fromIntegral will convert any integer type into any number type, floor, ceiling, and round will convert fractional types to integral types.
camccann
Check my edit plz, I am try to check if given number is Triangle Number
MMM
Thx for clue, now I did (ceiling x == floor x) and it seems to work ;) (false if x has factional part)
MMM
@MMM: Okay, I'm still confused, but glad I could help...?
camccann
Why confused? My problem was: user give number s, so I check if for s = n(n + 1)/2, n is natural, with none factional part, if so, given "s" is Triangle number... am I right?:)
MMM
@camccann: regarding "modulo function only makes sense on integers": http://reference.wolfram.com/mathematica/ref/PolynomialRemainder.html, and in Python you can use modulo like `7.2 % 5` and it might be useful for calculating what change you need to give in coins etc.
yairchu
@yairchu: A fair point on polynomials, though I don't know if I've heard the term "modulo" applied to that case. I expect other algebraic structures support something analogous, as well. The Python example is a bit silly, though, and really stretches the definition of "modular arithmetic". Then again, programming languages have been misusing that term for a while now...
camccann
@camccann: Python's "extension" of modulo makes sense as it is the only one that satisfies the rule `x == x//d + x%d`. I can see how it is in a way weird as unlike the other modulos it's not `a -> a -> a`
yairchu
@camccann: btw, a use case for Python's fractional modulo: let's say you are playing a sampled drum-loop. to know the current position in the loop, you can use Python's modulo
yairchu
+2  A: 

A better way to check if a number is triangular is to generate a list of triangular numbers and then see if your candidate is in it. Since this is a learning problem I'm going to give hints rather than the answer.

Use a list comprehension to generate the triangular numbers.

Since they will be in order you can find out if you have gone past them.

An alternative approach if you are working with big numbers would be to use a binary search to narrow down the number of rows that might give rise to your candidate.

Paul Johnson
But that list will be inifinty? Haskell stop searching themselfs when nummber will be bigger than searching one?
MMM
Yes, the list will be infinite. And no, Haskell won't realise this unless you tell it. So you need to figure out how to make the search terminate.
Paul Johnson
By which measure is that way better? It's certainly more expensive than just using the formula. It's also almost certainly more code.
sepp2k
Floating point numbers can be tricky, especially when you are comparing them. It would be very easy to end up with 3.999999982 instead of 4.0, at which point your algorithm fails. Even if it seems to work most of the time, you never know if the next case is going to fail. So if you are dealing with integers then its best to find a way of doing the whole thing with integers.
Paul Johnson
+2  A: 

To determine whether a certain Float or Double is indistinguishable from an Integer in Haskell, use floor and ceiling together. Something like:

if floor n == ceiling n
  then "It was some integer."
  else "It's between integers."

There might also be some fancy stuff you can do with the float's representation in binary, exposed by the RealFloat typeclass:

http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#t%3ARealFloat

Jason Dusek
You were too late, I found that solutions themselfes, check my comment above ;)
MMM
Where you say 'themselfes' I think you mean 'myself'.
Jason Dusek