Best way to get 3/2 == 1.5 ?

S

Stefan Salewski

I really like to get "real/float" division results. I know that there
are cases where one may need integer results, but for most of my
applications I do not want it. For example for may cairo drawing test at
http://www.ssalewski.de/PetEd-Demo.html.en unwanted integer results was
a reason for some strange bugs.

For Python there was something like "from future import division" to fix
it. In Ruby we can use 3.to_f / 2 or 3.fdiv 2. I am not really happy
with that notation, because I should use that always to ensure that I
get always my desired result.

Is there a simple way to redefine division, like Pythons "from future
import division"?. I have found a few tricks with Google (which I, still
learning Ruby, do not really understand), but I am no expert, so I do
not want to break something with ugly redefinition tricks.

Best regards

Stefan Salewski
 
S

serialhex

[Note: parts of this message were removed to make it a legal post.]

# some code for u

# do this so your rational will stay rational
require 'mathn'

# now
a = 3/2 # if in irb you should get (3/2) as a reply

a.to_f # should give you 1.5

# and thats it!!

mathn is a library that unify's all the numbers and makes them play well
together, why it's not loaded by default IDFK... but whatever, nothing's
perfect right?

-hex
 
S

Stefan Salewski

# some code for u

# do this so your rational will stay rational
require 'mathn'

# now
a = 3/2 # if in irb you should get (3/2) as a reply

a.to_f # should give you 1.5

# and thats it!!

mathn is a library that unify's all the numbers and makes them play well
together, why it's not loaded by default IDFK... but whatever, nothing's
perfect right?

-hex

Indeed, now I remember that I have seen this suggestion during my google
searches, some months ago.

May work fine, but it was my impression that that will decrease
performance and increase storage consumption, maybe drastically?
Please note, I do not need the precision of true rationals, float
precision is fine for me. But integer division can produce strange bugs,
if we are working with GTK and Cairo variables -- GTK can be integer
like window size, while cairo is float most of the time. So I have done
some divisions, sometimes GTK integers by integer, with
forgetting .to_f. Resulting in undesired results.

Best regards

Stefan Salewski
 
T

Tony Arcieri

[Note: parts of this message were removed to make it a legal post.]

For what it's worth, I read something Matz said once along the lines that
3/2 should probably be 1.5.

That said, you can monkeypatch integer division to have different
semantics. I would definitely NOT recommend doing this, but it does show you
the power of Ruby:

class Fixnum
alias :divide :/

def /(n)
if self % n == 0
self.divide(n)
else
self.to_f / n
end
end
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top