Powered by Invision Power Board

 
    Reply to this topicStart new topicStart Poll

> Java wierdness [now all is right with the world], Something wrong with my compy maybe
Unspecified
Citrus
Posted: Nov 4 2005, 11:56 AM
Quote Post


not dead yet
[*][*][*][*]

Group Icon
Group: Members
Posts: 18197
Member No.: 145
Joined: 19-November 03

Status: (0d) [--]


Discovered this when writing a fairly basic program for university coursework, and running into bugs. Values of primitive type double are acting strange under certain conditions. Like this:

very simple code:

CODE
       double number = 0;
       while (number < 0.16)
       {
           number = number + 0.01;
           System.out.println("number = " + number);
       }


The seemingly inexplicable results are
CODE
number = 0.01
number = 0.02
number = 0.03
number = 0.04
number = 0.05
number = 0.060000000000000005
number = 0.07
number = 0.08
number = 0.09
number = 0.09999999999999999
number = 0.10999999999999999
number = 0.11999999999999998
number = 0.12999999999999998
number = 0.13999999999999999
number = 0.15
number = 0.16


Maybe a virus or something is making my Java thingies act wierd?

This post has been edited by Citrusponge on Nov 8 2005, 03:54 AM


--------------------
user posted image
PMEmail PosterAOLMSN
Top
United States
Xgoff
Posted: Nov 4 2005, 12:01 PM
Quote Post


<):|
[*][*][*][*][*]
[*][*]

Group Icon
Group: Members
Posts: 52341
Member No.: 24
Joined: 13-October 03

Status: (0d) [--]


maybe your computer can't add

is there some way to have fixed decimal places?

This post has been edited by Xgoff on Nov 4 2005, 12:02 PM


--------------------

This post may contain original research or unverified claims.
Please disregard the above information and contact an administrator.

DISCLAIMER: by sending me (xgoff) a private message, you agree to the directives and their terms specified henceforth:
DIRECTIVE 1 (APPLE): i may or may not reply promptly or at all; and there are no guarantees to the usefulness of the reply. i may not acknowledge whether i have even received your private message
DIRECTIVE 2 (CHILE CON CARNE): as per my view, "private" applies only to the initial transaction, and the material of your message may or may not be made public at my discretion; as this will more than likely be a post in the CCC or IRC, you may not be able to view it
DIRECTIVE 3 (FEATHER DUSTER): you must address me (xgoff) as "Sir Master Xgofficus his Highest and Most Awesome the Third"; failure to comply with this term may invoke one or both of the above directives, and i will leave a burning bag of **** on your doorstep
DIRECTIVE 4 (BOOTSTRAP): if you have read this disclaimer, please private message me promptly, in compliance with the above terms, so i can ensure you are capable of following directions you idiot
this concludes the test of the emergency disclaimer system, your scheduled programming will now continue. satisfaction guaranteed, and 100% cash back available under certain circumstances; restrictions may or may not apply within your place of residence
NOTICE: these directives and their terms may change at any time, without notice; as a private message transaction to myself assumes an understanding and full compliance of the above, you should ensure you are fully aware of the above terms at any point before sending a private message; any message received is assumed to have been sent in compliance with the above

QUOTE
(5:25:58 PM) Mikau: xgoff
(5:26:00 PM) Mikau: guess what
(5:26:04 PM) Xgoff: chicken butt
(5:26:09 PM) Mikau: **** you
PMEmail PosterUsers WebsiteAOLMSN
Top
United States
Retriever II
Posted: Nov 4 2005, 01:27 PM
Quote Post


Catalyst
[H][*][*][*][*]
[*][*][*][*][*]


Group Icon
Group: Members
Posts: 18290
Member No.: 52
Joined: 13-October 03

Status: (0d) [--]


That's the nature of floating point. It's imprecise with even well behaved decimal numbers. You'll have to round.


--------------------

PMEmail PosterUsers WebsiteICQAOLMSN
Top
United States
Retriever II
Posted: Nov 4 2005, 01:28 PM
Quote Post


Catalyst
[H][*][*][*][*]
[*][*][*][*][*]


Group Icon
Group: Members
Posts: 18290
Member No.: 52
Joined: 13-October 03

Status: (0d) [--]


In fact do your math in integer and divide after. Best solution.


--------------------

PMEmail PosterUsers WebsiteICQAOLMSN
Top
Unspecified
Citrus
Posted: Nov 5 2005, 11:21 AM
Quote Post


not dead yet
[*][*][*][*]

Group Icon
Group: Members
Posts: 18197
Member No.: 145
Joined: 19-November 03

Status: (0d) [--]


So this is something that's meant to happen? Java sux then >:[
You sure? I can't just use integers here for my program. Some of the inputs *have* to be type double. That's not up to me. I can't convert them into integers without the compiler shouting POSSIBLE LOSS OF PRECISION at me sad.gif
This especially sucks because without this problem my code would be perfect :<

*thinks of an idea which may or may not work*


--------------------
user posted image
PMEmail PosterAOLMSN
Top
United States
Xgoff
Posted: Nov 5 2005, 11:31 AM
Quote Post


<):|
[*][*][*][*][*]
[*][*]

Group Icon
Group: Members
Posts: 52341
Member No.: 24
Joined: 13-October 03

Status: (0d) [--]


QUOTE (Citrusponge @ Nov 5 2005, 09:21 AM)
So this is something that's meant to happen? Java sux then >:[
You sure? I can't just use integers here for my program. Some of the inputs *have* to be type double. That's not up to me. I can't convert them into integers without the compiler shouting POSSIBLE LOSS OF PRECISION at me sad.gif
This especially sucks because without this problem my code would be perfect :<

*thinks of an idea which may or may not work*

well your "possible loss of precision" would only be one one-hundred-quadrillionth off anyway so it probably wouldn't matter


--------------------

This post may contain original research or unverified claims.
Please disregard the above information and contact an administrator.

DISCLAIMER: by sending me (xgoff) a private message, you agree to the directives and their terms specified henceforth:
DIRECTIVE 1 (APPLE): i may or may not reply promptly or at all; and there are no guarantees to the usefulness of the reply. i may not acknowledge whether i have even received your private message
DIRECTIVE 2 (CHILE CON CARNE): as per my view, "private" applies only to the initial transaction, and the material of your message may or may not be made public at my discretion; as this will more than likely be a post in the CCC or IRC, you may not be able to view it
DIRECTIVE 3 (FEATHER DUSTER): you must address me (xgoff) as "Sir Master Xgofficus his Highest and Most Awesome the Third"; failure to comply with this term may invoke one or both of the above directives, and i will leave a burning bag of **** on your doorstep
DIRECTIVE 4 (BOOTSTRAP): if you have read this disclaimer, please private message me promptly, in compliance with the above terms, so i can ensure you are capable of following directions you idiot
this concludes the test of the emergency disclaimer system, your scheduled programming will now continue. satisfaction guaranteed, and 100% cash back available under certain circumstances; restrictions may or may not apply within your place of residence
NOTICE: these directives and their terms may change at any time, without notice; as a private message transaction to myself assumes an understanding and full compliance of the above, you should ensure you are fully aware of the above terms at any point before sending a private message; any message received is assumed to have been sent in compliance with the above

QUOTE
(5:25:58 PM) Mikau: xgoff
(5:26:00 PM) Mikau: guess what
(5:26:04 PM) Xgoff: chicken butt
(5:26:09 PM) Mikau: **** you
PMEmail PosterUsers WebsiteAOLMSN
Top
Unspecified
Citrus
Posted: Nov 5 2005, 12:01 PM
Quote Post


not dead yet
[*][*][*][*]

Group Icon
Group: Members
Posts: 18197
Member No.: 145
Joined: 19-November 03

Status: (0d) [--]


QUOTE (Xgoff @ Nov 5 2005, 04:31 PM)
well your "possible loss of precision" would only be one one-hundred-quadrillionth off anyway so it probably wouldn't matter

The compiler forces me to change it anyway :/

Doesn't matter now though, my slightly altered code works perfectly now. Thanks anyway biggrin.gif


--------------------
user posted image
PMEmail PosterAOLMSN
Top
Unspecified
Quicky
Posted: Nov 5 2005, 01:44 PM
Quote Post


a weed in glaber's garden
[*]

Group Icon
Group: Members
Posts: 12957
Member No.: 7
Joined: 13-October 03

Status: (0d) [--]


that's due to the round-off error, it's just a simple fact of binary that it can't hold numbers like 0.3 exactly without an infinite amount of space

some more basic programming languages will round it off transparently but java isn't one of them, so what you want to do to avoid this is check if the variable you're testing is almost exactly what you want, say in a range of about 0.0000000000000010

or you could just do what r-ii says and use integers more, but that's for pansies ;)

also while we're at it does java do fixed-point numbers too?

This post has been edited by Quicky on Nov 5 2005, 01:46 PM


--------------------
the only good userbar on earth:

PM
Top
Unspecified
Citrus
Posted: Nov 5 2005, 01:53 PM
Quote Post


not dead yet
[*][*][*][*]

Group Icon
Group: Members
Posts: 18197
Member No.: 145
Joined: 19-November 03

Status: (0d) [--]


Java has two types for real numbers, 'double' and 'float'.
My book says float is 'single-precision floating point' e.g. 43.889F
And double is 'double-precision floating point' e.g. 45.63 or 2.4e5

I have no idea what that means biggrin.gif

Except the F after the float specifies that it is indeed of type float.

I don't like floating points.

<<edit>>
For anyone who cares:

Float is even worse.

Similar, should-be-predictable code:

CODE
   float number = 0;
   while (number < 0.16)
      {
          number = number + 0.01F;
          System.out.println("number: " + number);
      }


prints this:

QUOTE
number: 0.01
number: 0.02
number: 0.03
number: 0.04
number: 0.049999997
number: 0.059999995
number: 0.06999999
number: 0.07999999
number: 0.08999999
number: 0.09999999
number: 0.109999985
number: 0.11999998
number: 0.12999998
number: 0.13999999
number: 0.14999999
number: 0.16
number: 0.17


soveryhappy.gif

This post has been edited by Citrusponge on Nov 5 2005, 02:02 PM


--------------------
user posted image
PMEmail PosterAOLMSN
Top
Unspecified
Quicky
Posted: Nov 5 2005, 02:00 PM
Quote Post


a weed in glaber's garden
[*]

Group Icon
Group: Members
Posts: 12957
Member No.: 7
Joined: 13-October 03

Status: (0d) [--]


real numbers in java are represented with the exponent-coefficient (real men call it the mantissa ;) model, which is explained right here

floats are 32-bit numbers with a 24-bit mantissa and an 8-bit exponent, doubles are 64-bit with a 53-bit mantissa and 11-bit exponent, so in short doubles give you much more precision and a wider range of numbers

man i'm on a roll

This post has been edited by Quicky on Nov 5 2005, 02:01 PM


--------------------
the only good userbar on earth:

PM
Top
Unspecified
Citrus
Posted: Nov 5 2005, 02:04 PM
Quote Post


not dead yet
[*][*][*][*]

Group Icon
Group: Members
Posts: 18197
Member No.: 145
Joined: 19-November 03

Status: (0d) [--]


Ooh, we learnt about mantissa and exponent and shi in college. We didn't learn that these silly problems come up :|

Yay Wikipedia


--------------------
user posted image
PMEmail PosterAOLMSN
Top
United States
Retriever II
Posted: Nov 5 2005, 02:43 PM
Quote Post


Catalyst
[H][*][*][*][*]
[*][*][*][*][*]


Group Icon
Group: Members
Posts: 18290
Member No.: 52
Joined: 13-October 03

Status: (0d) [--]


QUOTE (Quicky @ Nov 5 2005, 01:44 PM)
or you could just do what r-ii says and use integers more, but that's for pansies wink.gif

Take assembler-level programming and you'll learn real men use integers whenever possible. They're many times faster than floating point arithmetic.

And you can do fixed-point decimal in integer as well.

The only good way to escape round off error without always needing to round is to stay away from floating point arithmentic. Calculators use BCD arithmetic instead, which has no round-off error. But that's another beast I won't get into.


--------------------

PMEmail PosterUsers WebsiteICQAOLMSN
Top
Unspecified
Citrus
Posted: Nov 7 2005, 05:09 AM
Quote Post


not dead yet
[*][*][*][*]

Group Icon
Group: Members
Posts: 18197
Member No.: 145
Joined: 19-November 03

Status: (0d) [--]


Hey d00ds, I'm posting this from uni \o/

More Java funfunfun:

CODE
       double value = 0;
       int number = 0;
       while (value < 0.16);
       {
           System.out.println("value: " + value);
           number = (number + 1);
           value = (0.01 * number);
       }


I wrote this code to do the same thing as that other code, but hopefully without the wierd precision errors. I used a similar technique to make my assignment code work, and it worked just fine. But this code... does not work at all. It just makes the runtime environment act like it's trying to work until I kill it. I can't see any problems with it, and the compiler has no complaints. Anyone see anything bad about this?


--------------------
user posted image
PMEmail PosterAOLMSN
Top
United States
Retriever II
Posted: Nov 7 2005, 09:08 AM
Quote Post


Catalyst
[H][*][*][*][*]
[*][*][*][*][*]


Group Icon
Group: Members
Posts: 18290
Member No.: 52
Joined: 13-October 03

Status: (0d) [--]


I don't see anything wrong with it. You're sure that's the code causing the environment to hang?


--------------------

PMEmail PosterUsers WebsiteICQAOLMSN
Top
Unspecified
Citrus
Posted: Nov 7 2005, 11:53 AM
Quote Post


not dead yet
[*][*][*][*]

Group Icon
Group: Members
Posts: 18197
Member No.: 145
Joined: 19-November 03

Status: (0d) [--]


I'm pretty sure. The same thing happens on both the computer I was using at university and this home one here. I copy-pasted the code just now :/

Changing certain statements lets the code run as expected (but not as I wanted)... I'll experiment more with this.


--------------------
user posted image
PMEmail PosterAOLMSN
Top
United States
Retriever II
Posted: Nov 7 2005, 07:18 PM
Quote Post


Catalyst
[H][*][*][*][*]
[*][*][*][*][*]


Group Icon
Group: Members
Posts: 18290
Member No.: 52
Joined: 13-October 03

Status: (0d) [--]


Semicolons don't go after While () statements.
If that's a direct copy and paste from your program and not a copy error.


--------------------

PMEmail PosterUsers WebsiteICQAOLMSN
Top
United States
Retriever II
Posted: Nov 7 2005, 07:19 PM
Quote Post


Catalyst
[H][*][*][*][*]
[*][*][*][*][*]


Group Icon
Group: Members
Posts: 18290
Member No.: 52
Joined: 13-October 03

Status: (0d) [--]


If that did in fact run like that, that would EASILY cause your infinite loop of do-nothing.


--------------------

PMEmail PosterUsers WebsiteICQAOLMSN
Top
Unspecified
Citrus
Posted: Nov 8 2005, 03:52 AM
Quote Post


not dead yet
[*][*][*][*]

Group Icon
Group: Members
Posts: 18197
Member No.: 145
Joined: 19-November 03

Status: (0d) [--]


OOhhh silly me biggrin.gif

CODE
value: 0.0
value: 0.01
value: 0.02
value: 0.03
value: 0.04
value: 0.05
value: 0.06
value: 0.07
value: 0.08
value: 0.09
value: 0.1
value: 0.11
value: 0.12
value: 0.13
value: 0.14
value: 0.15


woohoo it counted right too \o/

This post has been edited by Citrusponge on Nov 8 2005, 03:53 AM


--------------------
user posted image
PMEmail PosterAOLMSN
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

  Topic Options Topic Options Reply to this topicStart new topicStart Poll

 




[ Script Execution time: 0.0700 ]   [ 14 queries used ]   [ GZIP Enabled ]   [ Server Load: 0.71 ]