Java- double variable issue -
here's simple equations counter.now counts equations 2 variables.it's based on trying many combinations (4 milion or 16 milion) of a
, b
variables.so code works , counts exact.but since tried change variable b
double, things go wrong.i've expected line b=b+0.1
secures setting variable b
each 10th time 1.0.but imidiately after start way more decimal numbers appear each b
.so use wrong datatype? or should raise variable b
different value?(i tried changing variables double).thank suggestions!
import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; public class buffer{ static int a; static double b; static bufferedreader reader; static string query; static int range; static int result; public static void main(string[] args)throws ioexception{ reader=new bufferedreader(new inputstreamreader(system.in)); system.out.print("choose speed of test(fast or slow):"); query=reader.readline(); if(query.equals("fast")) range=2000; else range=4000; //start test// while((a+b)!=26000){ b=b+0.1; if(b==range+1){ b=0; a=a+1; } if((a+b)!=26000) system.out.println("a- "+a+", "+"b- "+b+"=false."); if((a+b)==26000){ system.out.println("a- "+a+", "+"b- "+b+"=true."); system.out.println("\n"+"done.you can possibly solve equation, if a= "+a+" , b= "+b+"."); } if(a==range&&b==range&&(a+b)!=26000){ system.out.println("\n"+"tested "+a*b+" options.solution not found."); system.exit(0); } } } }
you can use bigdecimal instead of float/double primitives avoid issues. of described here: java mistake 1 - using float , double monetary or financial calculation
Comments
Post a Comment