Java:Double Datatype Value Doesn't Give Exact Answer -
this question has answer here:
i'm finding logarithm of value 1 equal 0 not 0.0. when user enter point values give exact answer. problem value not consist in points give answer of value in points.i try parsing , typecasting nothing happen.is there function in java can stop this.
code.
public class new { public static void main(string[] args) { scanner s = new scanner(system.in); double num = 0; double result; system.out.print("value:"); num = s.nextdouble(); result = math.log(num); system.out.print("answer:"+result); } }
compiler output:
value:1 answer:0.0
the 0.0
how double
value representing 0
printed default. also, math.log
method returns double
. 0.0
equal number 0. logarithm, positive base, of 1 0.
if you'd not print decimal point if result integer, test if it's integer.
if (result == (int) result) { system.out.println((int) result); } else { system.out.println(result); }
Comments
Post a Comment