Different values in a JAVA code -
i have code should value it:
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* name of class has "main" if class public. */ class ideone { public static void main (string[] args) throws java.lang.exception { float a=4, b=8 ; int i=2, j=16 ; double sol1, sol2, sol3, sol4; sol1=(a+2*b/j); sol2=(a/(b-4)+i); sol3=-(b-i/j); sol4=(i*a+j/b); system.out.println(sol1); system.out.println(sol2); system.out.println(sol3); system.out.println(sol4); } }
and here java fiddle of solution:
5 3 -8 10
but in sol3, calculated value not -8, -7.875, why takes -8.0.
i tried decompose sol3 alone separate execution like in fiddle:
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* name of class has "main" if class public. */ class ideone { public static void main (string[] args) throws java.lang.exception { float a=4, b=8 ; int i=2, j=16 ; double sol1, sol2, sol3, sol4; sol1=-i/j; sol2=b+sol1; sol3 = -sol2; system.out.println(sol1); system.out.println(sol2); system.out.println(sol3); } }
and still giving me -8.0 , on calculator -7.875. know easy can't figure out why ?
sol1=-i/j;
is evaluated 0 since < j , both of them int
. change 1 of them float or double in order perform floating point division.
Comments
Post a Comment