Results 1 to 7 of 7
 08042013, 05:13 PM #1Member
 Join Date
 Jul 2013
 Posts
 52
 Rep Power
 0
Removing negative value after truncating
The question is to print value for sin and cos for degree 0 to 360 increment by 10.
Below is the code i wrote but the problem is after the value sin 360 becomes 0.0000 instead of 0.0000 after limiting to 4 decimal places(which is required by the question). How to solve this issue?
Issit should be done by hardcoding if (value < 0.00000001, Math.abs(value)?
or should I used value * 10000 / 10000 to limit to 4 decimal places and use decimal format to display the trailing zero?
Java Code:public static void main(String[] args) { for(int degree = 0; degree <= 360; degree += 10){ System.out.print(degree + "\t\t"); System.out.printf("%.4f", Math.sin(degree * Math.PI / 180)); } }
Last edited by dojob; 08042013 at 05:15 PM.
 08042013, 05:22 PM #2
 08042013, 05:33 PM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,952
 Blog Entries
 7
 Rep Power
 22
Re: Removing negative value after truncating
You and I know that sin(x) == sin(k*360+x) but the poor computer doesn't know that; why not help it a bit?
Java Code:public static void main(String[] args) { for(int degree = 0; degree <= 360; degree += 10){ System.out.print(degree + "\t\t"); int d= degree%360; System.out.printf("%.4f", Math.sin(d * Math.PI / 180)); } }
JosI have the stamina of a seal; I lie on the beach instead of running on it.
 08042013, 05:47 PM #4Member
 Join Date
 Jul 2013
 Posts
 52
 Rep Power
 0
Re: Removing negative value after truncating
It will convert the insignificant negative value to absolute value 0.000000001 and after that 0.000 will be display? But it doesn't look proper to code it this way.
I have tried using decimal formatter and it seems the negative value still around for insignificant negative value.
 08042013, 05:49 PM #5Member
 Join Date
 Jul 2013
 Posts
 52
 Rep Power
 0
 08042013, 05:50 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,952
 Blog Entries
 7
 Rep Power
 22
Re: Removing negative value after truncating
Last edited by JosAH; 08042013 at 05:51 PM. Reason: wrong IEEE number ..
I have the stamina of a seal; I lie on the beach instead of running on it.
 08042013, 05:57 PM #7
Similar Threads

Need help with truncating decimals?
By akeni in forum New To JavaReplies: 3Last Post: 10052011, 12:31 AM 
Mod of Negative Numbers
By Venny in forum New To JavaReplies: 7Last Post: 01282011, 06:32 AM 
Help with negative numbers
By cela0811 in forum New To JavaReplies: 2Last Post: 01042011, 06:04 PM 
Negative elements
By swikar.java in forum New To JavaReplies: 6Last Post: 12152008, 05:48 AM 
Truncating decimal numbers in the output
By gbade in forum New To JavaReplies: 2Last Post: 11212008, 07:25 PM
Bookmarks