Heh. You can’t express 1/10 accurately in binary (just like you can’t epxress 1/3 accurately in base 10). So the FP representation of 4.10 is going to be 4.09999…
Multiplying that times 100 gives you 409.999…
Apparently to_i doesn’t do any rounding. That isn’t what I’d expect, really. But it does make sense.
Bob27 Sep 13:41
“Apparently to_i doesn’t do any rounding.”
Nor should it.
Vincent Foley27 Sep 13:42
> (* 4.10 100.0)
409.99999999999994
>
That’s from Scheme. It’s the inexactitude of floats. I guess the Forth guys were on to something when they were using just fixed numbers, no floats ;)
Matt Moriarity27 Sep 13:46
The funny thing is 4.10 * 100.0 returns 410.0
Andy Keep27 Sep 13:47
Fortunately you can always go for… (4.10 * 100.0).ceil
I would have expected to_i to follow standard rounding rules though.
“I would have expected to_i to follow standard rounding rules though.”
Casting a float to an integer means you lose all the information after the decimal. Rounding should not occur in this case. Consider the following C code:
printf(“%d\n”, (int)(4.10 * 100.0));
If it’s that important, I guess you could always do:
class Float
def to_i
round
end
end
greasygreasy27 Sep 14:09
So why doesn’t 4.10 * 100.0 return 409.999999999999?
Course… it helps if you have a preview button. (Which the latest version of Typo does, though it’s a hyperlink instead of a button, which is really wierd.)
If you want to convert a Float to the nearest Fixnum, use
<code>round</code>. The <code>to_i</code> method truncates float,
i.e., discards the fractional part.
As is often the case, ri is most helpful here:
<pre><code>$ ri ‘Float#round’
flt.round => integer
Rounds flt to the nearest integer....
$ ri ‘Float#to_i’
</code></pre>
flt.to_i => integer
flt.to_int => integer
flt.truncate => integer
Returns flt truncated to an Integer.
And this is why all computer programmers need to understand C: It gives you a familiarity with the hardware, so you can understand what happens in cases like this.
Marten 27 Sep 13:25
irb(main):049:0> (4.10 * 100) – 410 => -5.6843418860808e-14
... :/
Scott Laird 27 Sep 13:26
Heh. You can’t express 1/10 accurately in binary (just like you can’t epxress 1/3 accurately in base 10). So the FP representation of 4.10 is going to be 4.09999…
Multiplying that times 100 gives you 409.999…
Apparently to_i doesn’t do any rounding. That isn’t what I’d expect, really. But it does make sense.
Bob 27 Sep 13:41
“Apparently to_i doesn’t do any rounding.”
Nor should it.
Vincent Foley 27 Sep 13:42
> (* 4.10 100.0) 409.99999999999994 >
That’s from Scheme. It’s the inexactitude of floats. I guess the Forth guys were on to something when they were using just fixed numbers, no floats ;)
Matt Moriarity 27 Sep 13:46
The funny thing is 4.10 * 100.0 returns 410.0
Andy Keep 27 Sep 13:47
Fortunately you can always go for… (4.10 * 100.0).ceil
I would have expected to_i to follow standard rounding rules though.
cDlm 27 Sep 13:53
st> 4.10 * 100.0! 410.000000000000 st> (4.10 * 100.0) / 100.0! 4.10000000000000 st> 410 / 100! Fraction new ”<0×2037a08>” st> (410 / 100) asFloat! 4.10000000000000
And that’s from (GNU) Smalltalk :)
Bob 27 Sep 13:59
“I would have expected to_i to follow standard rounding rules though.”
Casting a float to an integer means you lose all the information after the decimal. Rounding should not occur in this case. Consider the following C code: printf(“%d\n”, (int)(4.10 * 100.0));
If it’s that important, I guess you could always do: class Float def to_i round end end
greasygreasy 27 Sep 14:09
So why doesn’t 4.10 * 100.0 return 409.999999999999?
Robby Russell 27 Sep 14:32
<type:code lang=”ruby”> irb(main):005:0> x = 4.10 * 100 => 410.0 irb(main):006:0> x.to_i => 409 </typo:code>
Robby Russell 27 Sep 14:33
ruby can’t scale
;-)
Jim Helm 27 Sep 14:47
Can somone explain why (4.1100.0).to_i and (5.1100.0).to_i result in 409 and 509, but 1..3 and 6..9 all give the “correct” answer?
Dominik Wagner 27 Sep 15:01
the same in python: >>> 4.10 * 100 409.99999999999994 >>> 410 / 100. 4.0999999999999996
myiruka@yahoo.co.jp 27 Sep 15:08
irb(main):001:0> (4.10 * 1000.0).to_i
Whadya think, 4099 or 4100?
Chris 27 Sep 15:22
So:
irb(main):003:0> (4.10100.0).to_i => 409
Hmm. And:
irb(main):006:0> x = 4.10 * 100.0 => 410.0 irb(main):007:0> x.to_i => 409
But:
irb(main):001:0> 4.10100.0 => 410.0 irb(main):002:0> 410.0.to_i => 410
This doesn’t make sense, unless by “sense” you mean “batshit crazy implementation detail.”
Chris 27 Sep 15:23
Since when does anyone * apply emphasis across lines ? *
I think typo’s formatting heuristic may need a small improvement. :)
Bob Aman 27 Sep 15:32
That’s what <notextile> blocks are for.
Bob Aman 27 Sep 15:33
Course… it helps if you have a preview button. (Which the latest version of Typo does, though it’s a hyperlink instead of a button, which is really wierd.)
Mike 27 Sep 16:33
This pretty much explains it:
irb(main):014:0> ’%.16f’ % (4.10100.0) => “409.9999999999999432”
irb(main):016:0> ’%.16f’ % (4.10100.0).truncate => “409.0000000000000000”
That’s what Float#to_i does
Mike 27 Sep 16:35
<notextile> This pretty much explains it:
irb(main):003:0> ’%.16f’ % (4.10 * 100.0) => “409.9999999999999432” irb(main):004:0> ’%.16f’ % (4.10 * 100.0).truncate => “409.0000000000000000”
That’s what Float#to_i does </notextile>
Mike 27 Sep 16:36
I give up :-)
Jim Weirich 27 Sep 17:21
<notextile> > (4.1 * 100.0).round => 410 </notextile>
Kent 27 Sep 18:10
$ cat main.c #include <stdio.h>
main() { printf(“%d\n”, (int)(4.10 * 100.0)); } $ gcc -o main main.c $ ./main 409 $
Kent Sibilev 27 Sep 18:12
<notextile> $ cat main.c #include <stdio.h>
main() { printf(“%d\n”, (int)(4.10 * 100.0)); } $ gcc -o main main.c $ ./main 409 $ </notextile>
Tom Moertel 27 Sep 18:33
If you want to convert a Float to the nearest Fixnum, use <code>round</code>. The <code>to_i</code> method truncates float, i.e., discards the fractional part.
As is often the case, ri is most helpful here:
<pre><code>$ ri ‘Float#round’
$ ri ‘Float#to_i’ </code></pre>
—Tom
Dominique PERETTI 27 Sep 22:40
> php -r “print(intval(4.10 * 100.0));” > 409
:-D
Evan Jones 28 Sep 12:11
And this is why all computer programmers need to understand C: It gives you a familiarity with the hardware, so you can understand what happens in cases like this.
Bob Aman 28 Sep 16:46
Yay for new preview button!
Nick Van Weerdenburg 29 Sep 06:55
javascript: alert(4.10 * 100.0)
displays: 409.99999999999994
Which is actually correct according to some floating point specs. The browser purveyors say that this is not a bug.
Scotto 02 Oct 22:11
Aww man, poor Ruby.
test 03 Oct 15:47
test ajax
test2 04 Oct 03:02
2nd ajax test
null 04 Oct 17:00
Thanks!
vWing 05 Oct 11:22
Maybe do it like this:
irb> require ‘mathn’
irb> ((4+1/10)*100).to_i
irb> 410
foo 05 Oct 11:49
foo
foo 05 Oct 18:01
bar
foo 06 Oct 04:30
baared
hi 09 Oct 09:07
hello
peter 10 Oct 02:01
gdfgdf
null 10 Oct 02:58
hhhhh
alex 16 Oct 04:35
Hello, how are you?
d 18 Oct 13:12
d
techscam 18 Oct 21:51
?
mike 19 Oct 10:05
hi
ll 25 Oct 14:18
ll
Andrew 25 Oct 19:31
Checking out the instant comments.
efgdfgdfgd 07 Nov 16:55
fghfhdfg
myspace music video codes 07 Nov 22:57
Wtf is that suppose to mean!?
testit 23 Nov 16:13
now
tester 25 Nov 18:48
now
tester 25 Nov 18:50
test
tester 25 Nov 18:50
00
shilva 09 Dec 23:12
cooool
coward 11 Dec 16:06
yah
tester! 17 Dec 00:02
cute ladybug. i am sorry for ‘testing’ comments.