can someone explain this to me:
puts "#{1210 / 100}" --> 12
why isn’t the answer 12.1?
i was getting ticked at javascript for producing poor arithmetic results but
this is nuts!
-transami
can someone explain this to me:
puts "#{1210 / 100}" --> 12
why isn’t the answer 12.1?
i was getting ticked at javascript for producing poor arithmetic results but
this is nuts!
-transami
Hello Tom,
can someone explain this to me:
puts "#{1210 / 100}" --> 12
why isn’t the answer 12.1?
i was getting ticked at javascript for producing poor arithmetic results but
this is nuts!
-transami
I’m surprised you don’t know this, Tom. In Ruby, and several other
languages, the division of two integers results in an integer. To get
a floating-point answer, use
puts "#{1210.0 / 100} → 12.1
(You could use 100.0 as well/instead.)
Cheers,
Gavin [who just added this to the FAQ]
On Friday, December 27, 2002, 4:59:59 PM, you wrote:
Tom Sawyer transami@transami.net writes:
can someone explain this to me:
puts “#{1210 / 100}” → 12
why isn’t the answer 12.1?
Dividing 2 Fixnum objects yields another Fixnum. Make one of them a
Float to get what you want.
1210 / 100
#=> 12
(1210 / 100).class
#=> Fixnum
(Float(1210) / 100).class
#=> Float
Float(1210) / 100
#=> 12.1
“Tom Sawyer” transami@transami.net wrote in message
puts “#{1210 / 100}” → 12
why isn’t the answer 12.1?
Try:
puts “#{1210.0 / 100}” # => 12.1
^^
Without this .0 , Ruby takes it as integer division…
i was getting ticked at javascript for producing poor arithmetic results
but
this is nuts!
Relax
Because 1210 is an int, 100 is also an int. Therefore the result will be
an int (truncated).
What you want is this: puts “#{1210.0 / 100}”
Tom Sawyer wrote:
can someone explain this to me:
puts “#{1210 / 100}” → 12
why isn’t the answer 12.1?
i was getting ticked at javascript for producing poor arithmetic results but
this is nuts!-transami
–
Wai-Sun “Squidster” Chia
Consulting & Integration
Linux/Unix/Web Developer Dude
“Just Another Ruby Miner”
Tom Sawyer wrote:
can someone explain this to me:
puts “#{1210 / 100}” → 12
why isn’t the answer 12.1?
i was getting ticked at javascript for producing poor arithmetic results but
this is nuts!
puts (1210.00/100.00) => 12.1
You started with two integers so you got an integer result.
From: “Tom Sawyer” <transami@transami.net
can someone explain this to me:puts “#{1210 / 100}” → 12
why isn’t the answer 12.1?
i was getting ticked at javascript for producing poor arithmetic
results but this is nuts!
Hey, no reason for panic.
class Fixnum
def / ( rightSide )
return self.to_f / rightSide
end
end
puts “#{1210 / 100}” → 12.1
Regards,
M.
no i didn’t realize that. i thought ruby would automatically change it to a
float if need be, i.e. with divison, just as it changes to Bignum if need be.
honestly i don’t like it. now i have to go around adding .0’s all over the
place to make sure the math comes out right. and in some places i have to do
things like multiple by 1.0 b/c certain values come from cgi script variables
i have no control over.
well, it will suffice now that i know, but to me it is definitely not POLS.
-tom
On Thursday 26 December 2002 11:19 pm, Gavin Sinclair wrote:
Hello Tom,
On Friday, December 27, 2002, 4:59:59 PM, you wrote:
can someone explain this to me:
puts "#{1210 / 100}" --> 12
why isn’t the answer 12.1?
i was getting ticked at javascript for producing poor arithmetic results
but this is nuts!-transami
I’m surprised you don’t know this, Tom. In Ruby, and several other
languages, the division of two integers results in an integer. To get
a floating-point answer, useputs "#{1210.0 / 100} → 12.1
(You could use 100.0 as well/instead.)
Cheers,
Gavin [who just added this to the FAQ]
now THAT’s what i love about ruby!
very nice solution
On Thursday 26 December 2002 11:44 pm, Milan Maksimovic wrote:
Hey, no reason for panic.
class Fixnum
def / ( rightSide )
return self.to_f / rightSide
end
endputs “#{1210 / 100}” → 12.1
note to self:
.to_f not * 1.0
On Thursday 26 December 2002 11:27 pm, Tom Sawyer wrote:
no i didn’t realize that. i thought ruby would automatically change it to a
float if need be, i.e. with divison, just as it changes to Bignum if need
be.honestly i don’t like it. now i have to go around adding .0’s all over the
place to make sure the math comes out right. and in some places i have to
do things like multiple by 1.0 b/c certain values come from cgi script
variables i have no control over.
Hi Tom,
Integer division is one of those areas which is strangely
controversial. To some people, POLS is “integer divided by integer
results in an integer”. To others, it is “a number divided by another
number results in the integer or decimal value to represent the precise
quotient, converting between numeric types if necessary”.
If you can’t tell by my stumbling over the phrasing of this problem, I
belong in the first camp. For me in this case, POLS is sticking to the
initial types unless explicitly stating or absolutely required
otherwise. This behavior is similar to that of other languages, so
somebody coming in from one of those other languages would be surprised
if “1210/10” did not return 12!
-Brian W
On Thu, 2002-12-26 at 22:27, Tom Sawyer wrote:
no i didn’t realize that. i thought ruby would automatically change it to a
float if need be, i.e. with divison, just as it changes to Bignum if need be.honestly i don’t like it. now i have to go around adding .0’s all over the
place to make sure the math comes out right. and in some places i have to do
things like multiple by 1.0 b/c certain values come from cgi script variables
i have no control over.well, it will suffice now that i know, but to me it is definitely not POLS.
-tom
–
Brian Wisti
brian@coolnamehere.com
http://coolnamehere.com/
Hi –
On Fri, 27 Dec 2002, Tom Sawyer wrote:
On Thursday 26 December 2002 11:44 pm, Milan Maksimovic wrote:
Hey, no reason for panic.
class Fixnum
def / ( rightSide )
return self.to_f / rightSide
end
endputs “#{1210 / 100}” → 12.1
now THAT’s what i love about ruby!
very nice solution
Be careful with it, though, as it changes the behavior globally and
might cause unexpected behavior in libraries you use (or in other
people’s code if they use a library where you’ve done this).
David
–
David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav
Fri, 27 Dec 2002 15:42:30 +0900, Brian Wisti brian@coolnamehere.com pisze:
Integer division is one of those areas which is strangely
controversial. To some people, POLS is “integer divided by integer
results in an integer”. To others, it is “a number divided by another
number results in the integer or decimal value to represent the precise
quotient, converting between numeric types if necessary”.If you can’t tell by my stumbling over the phrasing of this problem, I
belong in the first camp.
Here is a different formulation.
The second camp: the result of an arithmetic operation is defined
by abstracting from the types, performing the operation on the
mathematical values, and expressing the correct result in some type
which fits.
The first camp: as the first camp, except of one special case:
division of integers. There not only the value matters, but also the
type, and for integers it means something different than for floats
(truncating division rather than real division).
In a statically typed language it would not be a big flaw, because the
meaning of an operation is known statically. Passing 5 to a function
expecting a float performs the conversion.
In Ruby it means that integers are substitutable for floats in all
cases except division.
–
__("< Marcin Kowalczyk
__/ qrczak@knm.org.pl
^^ Blog człowieka poczciwego.
i do see your point, but i think it makes more sense for a strictly typed
langauge, rather than a dynamic one like ruby.
On Thursday 26 December 2002 11:42 pm, Brian Wisti wrote:
Integer division is one of those areas which is strangely
controversial. To some people, POLS is “integer divided by integer
results in an integer”. To others, it is “a number divided by another
number results in the integer or decimal value to represent the precise
quotient, converting between numeric types if necessary”.If you can’t tell by my stumbling over the phrasing of this problem, I
belong in the first camp. For me in this case, POLS is sticking to the
initial types unless explicitly stating or absolutely required
otherwise. This behavior is similar to that of other languages, so
somebody coming in from one of those other languages would be surprised
if “1210/10” did not return 12!
Also, if 1210 / 10 == 12.1 is your DESIRED and EXPECTED behavior for the
project you are working on, use the beauty of Ruby and simply alter
class Fixnum and be done with it.
class Fixnum
def /(n)
to_f / n
end
end
Beats adding 0’s all over the place
(Note1: tested in Ruby 1.73, but should be backwards compatible.)
(Note2: for projects under your direct control only; such “fixes” will
break POLS for experienced Rubyists!!!)
Regards,
Kent Starr
On Fri, 2002-12-27 at 01:37, Tom Sawyer wrote:
On Thursday 26 December 2002 11:27 pm, Tom Sawyer wrote:
no i didn’t realize that. i thought ruby would automatically change it to a
float if need be, i.e. with divison, just as it changes to Bignum if need
be.honestly i don’t like it. now i have to go around adding .0’s all over the
place to make sure the math comes out right. and in some places i have to
do things like multiple by 1.0 b/c certain values come from cgi script
variables i have no control over.note to self:
.to_f not * 1.0
just as you can substitute bytes for characters except
when using asian languages or unicode.
s.
On Sun, 5 Jan 2003 06:19:29 +0900, Marcin ‘Qrczak’ Kowalczyk qrczak@knm.org.pl wrote:
In Ruby it means that integers are substitutable for floats in all
cases except division.
And then one could argue that 2/3 should return an instance of “Rational”
which correctly represents the Quotient 2/3 and neither 0.666* in a
language like Ruby. Even 10/100 can lead to strang results because 1/10
is periodic to a base of 2 (which most modern computers work with).
-billy.
On Fri, Dec 27, 2002 at 04:10:59PM +0900, Tom Sawyer wrote:
On Thursday 26 December 2002 11:42 pm, Brian Wisti wrote:
If you can’t tell by my stumbling over the phrasing of this problem, I
belong in the first camp. For me in this case, POLS is sticking to the
initial types unless explicitly stating or absolutely required
otherwise. This behavior is similar to that of other languages, so
somebody coming in from one of those other languages would be surprised
if “1210/10” did not return 12!i do see your point, but i think it makes more sense for a strictly typed
langauge, rather than a dynamic one like ruby.
–
Meisterbohne Söflinger Straße 100 Tel: +49-731-399 499-0
eLösungen 89077 Ulm Fax: +49-731-399 499-9
that’s very different. and ideally we would all be using a single world-class
character encoding.
-transami
On Saturday 04 January 2003 03:36 pm, Stefan Schmiedl wrote:
On Sun, 5 Jan 2003 06:19:29 +0900, > > Marcin ‘Qrczak’ Kowalczyk qrczak@knm.org.pl wrote:
In Ruby it means that integers are substitutable for floats in all
cases except division.just as you can substitute bytes for characters except
when using asian languages or unicode.
Philipp Meier meier@meisterbohne.de writes:
If you can’t tell by my stumbling over the phrasing of this problem, I
belong in the first camp. For me in this case, POLS is sticking to the
initial types unless explicitly stating or absolutely required
otherwise. This behavior is similar to that of other languages, so
somebody coming in from one of those other languages would be surprised
if “1210/10” did not return 12!i do see your point, but i think it makes more sense for a strictly typed
langauge, rather than a dynamic one like ruby.And then one could argue that 2/3 should return an instance of “Rational”
which correctly represents the Quotient 2/3 and neither 0.666* in a
language like Ruby. Even 10/100 can lead to strang results because 1/10
is periodic to a base of 2 (which most modern computers work with).-billy.
I remember back in 1969 in my Introduction to Computer Programming
course (or whatever it was called), some of the first things we learned
had to do with fixed point and floating point arithmetic. We spent a
good month or so on these and other basic concepts before we were given
our first program to write.
Do they even teach these basics any more?
I’m not trying to disparage anyone here; if something hasn’t been
taught, then no one can be expected to have learned it.
But it’s disturbing to me to see the number of people who are posting in
this and other newsgroups in recent years who haven’t been exposed to
the concepts of fixed-point and floating-point arithmetic. This
indicates to me that there are many schools these days which don’t make
the effort to teach these basic concepts to their computer science
students. The fault is with these schools, and that’s sad.
On Fri, Dec 27, 2002 at 04:10:59PM +0900, Tom Sawyer wrote:
On Thursday 26 December 2002 11:42 pm, Brian Wisti wrote:
–
Meisterbohne Söflinger Straße 100 Tel: +49-731-399 499-0
eLösungen 89077 Ulm Fax: +49-731-399 499-9
–
Lloyd Zusman
ljz@asfast.com
really? you subsitute one thing for something other which
hasl almost the same behaviour … except where it counts.
and ideally we’d all use machines smart enough to implement
mathematical concepts like sqrt(2) exactly, not only approximately.
s.
On Sun, 5 Jan 2003 08:08:08 +0900, Tom Sawyer transami@transami.net wrote:
On Saturday 04 January 2003 03:36 pm, Stefan Schmiedl wrote:
On Sun, 5 Jan 2003 06:19:29 +0900, >> >> Marcin ‘Qrczak’ Kowalczyk qrczak@knm.org.pl wrote:
In Ruby it means that integers are substitutable for floats in all
cases except division.just as you can substitute bytes for characters except
when using asian languages or unicode.that’s very different. and ideally we would all be using a single world-class
character encoding.