Christ.
1
Name:
Anonymous
2009-11-18 22:37
In Python, why the fuck does float(2/3) return 0.0?
2
Name:
Anonymous
2009-11-18 22:41
That happens in *all* languages, integer division, /thread.
3
Name:
Anonymous
2009-11-18 22:59
>>2
Not really.
I my
Python (a Common Lisp compiler used by CMUCL, and from which SBCL is derived, which is what I'm using here) :
CL-USER> 2/3
2/3
CL-USER> (* * 3)
2
CL-USER> (float 2/3)
0.6666667
CL-USER> (* * 3)
2.0
4
Name:
Anonymous
2009-11-18 23:07
>>> float(2/3)
0.6666666666666666
Guess you're Python is just ancient. Try upgrading.
5
Name:
Anonymous
2009-11-18 23:47
>>4
I'm using 2.6. If you're subtly telling me to go to 3000, you should eat your hat.
I just got around it by adding 0.0000 to each
6
Name:
Anonymous
2009-11-18 23:49
Oh yeah, I forgot they changed this in Py3k. The old behaviour is 2//3.
Kinda stupid. Not sure how I feel about it.
7
Name:
Anonymous
2009-11-18 23:56
Kinda fucking stupid that the language has been split up in a level of retardation only before seen in the D programming language.
8
Name:
Anonymous
2009-11-19 0:23
>>1
Are you
DEMENTED ‽
Fuck you, OP. Fuck you.
9
Name:
Anonymous
2009-11-19 0:45
Prelude> 2/3 :: Float
0.*** Exception: stack overflow
:(
10
Name:
Anonymous
2009-11-19 1:14
$ echo "2/3" | bc ; echo "herpa derpa doooooo" ; echo "scale = 6 ; 2/3 " | bc
0
herpa derpa doooooo
.666666
11
Name:
Anonymous
2009-11-19 10:21
>>7
How so?
The only thing that I can think of that you might be referring to is
IHBT .
12
Name:
Anonymous
2009-11-19 11:29
>>5
I just got around it by adding 0.0000 to each
It's a good thing you added '0.0000', and to each as well. If you were to add 0.000, or, God forbid, 0.00, not to speak about further unthinkable reductions, who knows what might have happened?! Worst of all, if you add things asymmetrically, never do this.
13
Name:
Anonymous
2009-11-19 12:24
Use abs(2j / 3j), or if you've got a recent version, from __future__ import division
14
Name:
Anonymous
2009-11-19 13:04
Prelude> map (logBase 2 . fact) [170, 171]
[1019.3694529277262, Infinity]
lol'd.
16
Name:
Anonymous
2009-11-19 14:16
Christ, in python,lisp,C,java,pascal, why does 2/3 return 0?
17
Name:
Anonymous
2009-11-19 14:19
>>12
I wasn't adding asymmetrically, and good lord you care a lot.
18
Name:
Anonymous
2009-11-19 14:21
>>16
lisp
swing and a miss
19
Name:
Anonymous
2009-11-19 14:48
>>15
<interactive>:1:38: Not in scope: type constructor or class `CReal'
21
Name:
Anonymous
2009-11-19 15:37
>>20
I'm stupid. Anyway, it's not in the standard GHC installation.
23
Name:
Anonymous
2009-11-19 16:34
~ % ghci
GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
imLoading package base ... linking ... done.
poPrelude> import Data.Number.CReal
Could not find module `Data.Number.CReal':
Use -v to see a list of the files searched for.
Prelude> 2/3
0.***** Exc*** Exception: stack o*** Exception: stack ove*** Exception: stack overflowrflowverflowep*** Exc*** Exception: stack overfloweption: stack overflowtion: stack overflow* Exc*** Exception: stack overfloweption: st*** Exception: stack overflowack o*** Exception: stack overflowverflow
24
Name:
Anonymous
2009-11-19 17:23
>>23
look at me I made a funny
26
Name:
Anonymous
2009-11-19 19:34
27
Name:
Anonymous
2009-11-19 19:39
$ ghci
GHCi, version 6.10.4: http://www.haskell.org/ghc/ :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer ... linking ... done.
Loading package base ... linking ... done.
Prelude> 2 / 3
0.6666666666666666
Prelude> :t 2 / 3
2 / 3 :: (Fractional t) => t
Prelude>
I love that Haskell has polymorphic literals.
29
Name:
Anonymous
2009-11-19 19:51
float(2/3) divides the integers 2 and 3(resulting in 0) and then converting that 0 into a float(0.0)
31
Name:
Anonymous
2009-11-19 20:48
>>30
Totally, dude.
Prelude> fromInteger (2 `div` 3) :: Float
0.0
32
Name:
Anonymous
2009-11-19 20:55
>>28
Why not? It's pretty awesome to me. Why have silly rules about literals--like that floats must be specified with a period, even when there's nothing after the decimal--when the compiler can infer the meaning for you?
33
Name:
Anonymous
2009-11-19 21:17
Why force things to be decimal-values in the first place?
guile> (/ 2 3)
2/3
Behold, the way things ought to be.
34
Name:
Anonymous
2009-11-20 3:09
>>33
Yes, just like I've shown in
>>3
35
Name:
Anonymous
2009-11-20 12:51
>>34
And now we have come full circle.