[Chicago] Can anyone explain this behavior in Java?

Garrett Smith g at rre.tt
Mon Mar 23 17:16:40 CET 2015


You're not seeing a bug - this is expected behavior.

http://floating-point-gui.de/

http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

On Mon, Mar 23, 2015 at 9:59 AM, Lewit, Douglas <d-lewit at neiu.edu> wrote:
> Not my left foot or right foot, it's my nose that's itching!   ;-)   I
> actually went over my program and printed out the intermediate values
> (values BEFORE any computation of the probability).  Interestingly enough,
> all the values look just fine, so I'm still scratching my head on this one.
> My professor never replied to my email, so this is where I'll have to get a
> little aggressive and just corner him in his office during office hours.
>
> I actually contemplated sending my code to Oracle, but I'm sure I'll get a
> reply like, "Do you think this is Homework Central?  Don't bother us with
> such trivia!!!"  But hey, why not?  Worst case scenario, they'll just ignore
> me.
>
> The only thing I found on Stack Overflow was something to the effect of
> "double division can produce roundoff errors", and that was a segue into the
> BigDecimal class.  We'll see.
>
> In Python, this problem is very simple to do.  In Java, even a simple walk
> in the park gets transformed into a Thanksgiving Day parade!
>
> On Mon, Mar 23, 2015 at 7:43 AM, Jeremy McMillan <jeremy.mcmillan at gmail.com>
> wrote:
>>
>> Doug, don't throw away Java, but do abandon java questions on the Python
>> mailing list.
>>
>> Might we suggest you inquire at an appropriate forum?
>>
>> Chicago Java Users Group (Chicago, IL) - Meetup
>>
>>
>> Actually, I think your questions might get the best reception from
>> http://stackoverflow.com/
>>
>> Please repost there, and try to restrain your urges to post off topic. I
>> want to help, but after not too many empirical trials the probability that I
>> will discover something Python related to address in your posts gets
>> vanishingly small, and I'm likely to skip them without reading them. That
>> would be a waste, because you seem to spend a lot of time making them. Your
>> right foot is itching, but you're scratching your left foot.
>>
>> On Mon, Mar 23, 2015 at 1:49 AM, Lewit, Douglas <d-lewit at neiu.edu> wrote:
>>>
>>> Hey guys,
>>>
>>> I'm pretty impressed with Python's dictionary data structure, so I did
>>> some research and found out that Java has something equivalent called a
>>> "HashMap".  (Is that the same as a hash table or lookup table?  I guess it
>>> is.)  I'm also interested in simulating games of chance, so I decided to
>>> write a couple programs to simulate the game of rolling four dice.  (In
>>> statistics textbooks they always talk about rolling two dice, but that gets
>>> really boring after a while.  Why not three or four dice, right?)  If you
>>> roll four dice and record the sums, the sums can range from 4 (so you rolled
>>> all 1's) to 24 (which means you rolled all 6's).  The domain or sample space
>>> for this problem thus includes all integers from 4 to 24, inclusive.  But
>>> then you have to compute the probability values.  I decided to compute
>>> empirical probabilities by creating a random object that simulates throwing
>>> a die.  Pretty simple, right?
>>>
>>> I'm attaching two different but very closely related .java files.  In one
>>> I just use an array to store the probabilities.  In the other I use the Java
>>> version of a dictionary or "HashMap".  Arrays are great, but with an array
>>> all the keys are sequentially ordered integers starting at 0.  Well for this
>>> problem I don't want to start at 0; I want to start at 4, so it makes more
>>> sense to use some type of dictionary, right?
>>>
>>> Everything compiles and runs just great!  That's not the problem.  The
>>> problem is this.  If you look at my program that uses a HashMap, check out
>>> the empirical probabilities.  The final digits of the decimal are just
>>> screwed up!  For example, let's say that I run my simulation 1,000,000
>>> times, and in 1,000,000 throws of four dice I get a sum of 4 775 times,
>>> okay?  So the probability for rolling a 4 should be 0.000775, right?  When I
>>> use an array, that's exactly what I get.  But with a HashMap I'm getting
>>> 0.0007750000775.  ?????   What the hell is that?  I tried using
>>> System.out.printf and System.out.format but they don't work!  I'm really at
>>> a loss.  I just don't get it.  It's not a big tragedy and the program still
>>> basically works, but I guess I'm disappointed because my first HashMap
>>> program has a bug in it!  If anyone can shed light on this bug, hey, then
>>> please enlighten me cause I can't figure it out.
>>>
>>> Take it easy and thanks for the suggestions.
>>>
>>> Best,
>>>
>>> Douglas Lewit
>>>
>>> P.S.  I can just hear someone out there saying, "Doug, throw Java away
>>> and just stick with Python!"  While that is extremely tempting, right now
>>> that's not an option.  I want to pass my Java course with at least a B!
>>> :-)
>>>
>>>
>>> _______________________________________________
>>> Chicago mailing list
>>> Chicago at python.org
>>> https://mail.python.org/mailman/listinfo/chicago
>>>
>>
>>
>> _______________________________________________
>> Chicago mailing list
>> Chicago at python.org
>> https://mail.python.org/mailman/listinfo/chicago
>>
>
>
> _______________________________________________
> Chicago mailing list
> Chicago at python.org
> https://mail.python.org/mailman/listinfo/chicago
>


More information about the Chicago mailing list