Problem with Math operations

Hello. I need to validate the content of a JTextfield. It has to be a number between a MAX and a MIN in certain increases.

For example: MIN: 1.0 MAX: 1.6 INC.: 0.2

This means 1.0, 1.2, 1.4 and 1.6 would be valid

I have a function to valida this (it returns if the number is invalid):

privateboolean invalidField(double value,double min,double max,double inc){

if ((value>= min)&& (value<=max)){

if ( ( ( value - min ) % inc ) ==0){

returnfalse;

}else{

returntrue;

}

}else{

returntrue;

}

}

The Problem is that when I enter these values:

MAX: 2.0MIN: 1.0INC: 0.1VALUE: 1.5

the result of the operacion ( ( value - min ) % inc ) is not 0.0 and I should be.... because 1.5-1.0 = 0.5 AND 0.5%0.1 should be 0, but that is not the result I get!!!1

[1669 byte] By [Desireea] at [2007-10-2 20:57:51]
# 1

You probably never want to use == to compare floating point numbers, for one.

I assume the value that you got that wasn't what you expected (next time, paste in the unxpected results) was something like 0.0000000001. If not, there's a bug in your logic--I'm not going to look through your code for that.

If you did get something like the above number, though, read these:

[url=http://java.sun.com/developer/JDCTechTips/2003/tt0204.html#2]SOME THINGS YOU SHOULD KNOW ABOUT FLOATING-POINT ARITHMETIC[/url]

[url=http://docs.sun.com/source/806-3568/ncg_goldberg.html]What Every Computer Scientist Should Know About Floating-Point Arithmetic[/url]

Another good (slightly simpler) FP explanation:

http://mindprod.com/jgloss/floatingpoint.html

jverda at 2007-7-13 23:42:34 > top of Java-index,Java Essentials,Java Programming...
# 2
It may be the problem with the precision with which the floating point nos are represented. as your nos are 0.5 , 0.1 I don抰 really suggest the use of % operator with floats.There are other memebers here they will see this post and tell you
CjavaVMa at 2007-7-13 23:42:34 > top of Java-index,Java Essentials,Java Programming...
# 3

Gotcha .... (from Jverds link prev post)

The computer floating point unit works internally in base 2, binary. The decimal fraction 0.1 cannot be precisely represented in binary. It is a repeater fraction 0.00011001100110?It is like the repeater fraction 1/3 = 0.33333 in base 10. When you add 0.333333?to 0.666666?why are you not astonished to get 0.999999?rather than 1.0, even though you just added 1/3 + 2/3 to get 1? Yet, with Java floating point you are astonished when you add 0.1 + 0.1 and get something other than 0.2. The same fundamental mathematical cause is at work. It is God's fault for not making decimal 0.1 a perfect fraction in binary notation. Mike Cowlishaw, the creator of NetRexx, blames computer hardware makers for using binary floating point. He figures computer chips should use decimal notation like humans. 1/3 is also a repeater in binary, 0.01010101010?so it too will misbehave in Java just the way it does with decimal arithmetic.

CjavaVMa at 2007-7-13 23:42:34 > top of Java-index,Java Essentials,Java Programming...
# 4

You probably need a comparison tollerance

private boolean invalidField(double value, double min, double max, double inc){

if ((value>= min)&& (value<=max)){

double delta = ( value - min ) % inc;

if (delta > 0.5*inc)

delta = delta - inc;

System.out.println(delta);

if ( (delta > -1.0e-12) && ( delta < 1.0e-12)){

return false;

}else{

return true;

}

}else{

return true;

}

}

sabre150a at 2007-7-13 23:42:34 > top of Java-index,Java Essentials,Java Programming...
# 5
Validate with BigDecimal.
YAT_Archivista at 2007-7-13 23:42:34 > top of Java-index,Java Essentials,Java Programming...