This site requires JavaScript, please enable it in your browser!
Greenfoot back
Kartoffelbrot
Kartoffelbrot wrote ...

2014/5/28

Problems with numbers symbols

Kartoffelbrot Kartoffelbrot

2014/5/28

#
Hello, again I have a problem. I want to check, wether a value is greater or equal to zero. Seems simple, but there is one problem. So far I did it that way:
1
2
3
4
5
int a;
//initialising a by calculating something
if( a >= 0 ){
//do something
}
But somethimes a is -0.0 or -Infinity. In both cases the following code shouldn't be invoked. Has someone an idea? Thanks
lordhershey lordhershey

2014/5/28

#
why not declare a like int a = -1; so just in case your calculation does not happen for some strange reason? What is the calculation you are using?
Kartoffelbrot Kartoffelbrot

2014/5/28

#
double a = (xOfFirstPoint - xOfSecondPoint) / vec.dx(); vec is a vector. So somethimes my vector is vertivally, than vec.dx() is 0 or -0 (dependend if the vector goes up or downwards). Sometimes my the difference ((xOfFirstPoint - xOfSecondPoint)) is 0, so i get -Infinity or Inifinity.
lordhershey lordhershey

2014/5/28

#
what happens if vec.dx() is zero? Might want to make sure it is not zero and not really close to zero, it can mess up your numbers pretty bad.
Kartoffelbrot Kartoffelbrot

2014/5/28

#
If vec.dx() is 0 or -0 a is NaN. In that case I advise the method to invoke the code, too. Vec.dx() is zero if my object walks straight up or downwards.
lordhershey lordhershey

2014/5/28

#
Comparing Doubles can be a little weird try this loop here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import java.lang.*;
 
public class simpleLoop{
 
  public static void main(String args[])
  {
    double i = 0.0;
 
 
    while(i != 1)
    {
      System.out.println("i is " + i);
      i = i + 0.1;
    }
  }
}
run it and you would think this should stop after the program prints 0.9, but in fact this will run forever: i is 0.0 i is 0.1 i is 0.2 i is 0.30000000000000004 i is 0.4 i is 0.5 i is 0.6 i is 0.7 i is 0.7999999999999999 i is 0.8999999999999999 i is 0.9999999999999999 i is 1.0999999999999999 i is 1.2 i is 1.3 i is 1.4000000000000001 i is 1.5000000000000002 i is 1.6000000000000003 i is 1.7000000000000004 i is 1.8000000000000005 i is 1.9000000000000006 i is 2.0000000000000004 i is 2.1000000000000005 ....
Kartoffelbrot Kartoffelbrot

2014/5/28

#
Oh! So is there a possibility to distinguish between -0.0 and 0.0?
lordhershey lordhershey

2014/5/28

#
In the floating point world there is a bit difference between -0.0 and 0.0 the point of pointing out the loop is that you really cannot be sure what is being seen unless you look at the bit pattern representing the double. Double are 8 bytes IEEE 64 Bit Double Precision Floating Point Format you might be able to devise more reliable tests using bit masks, but I would recommend using test against Integer types since there is no grey area for those.
Kartoffelbrot Kartoffelbrot

2014/5/31

#
But what if my vectors amount is between 0 and 1? Is float a better datatype in this case?
lordhershey lordhershey

2014/5/31

#
I would multiply it with a large in like say you care about 4 decimal places - time the number by 1000 take the integer part of it then test on various sub-ranges since there is no ambiguity with integers.
Kartoffelbrot Kartoffelbrot

2014/5/31

#
Nice idea!
You need to login to post a reply.