Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By JosAH

Thread: Removing negative value after truncating

  1. #1
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default 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; 08-04-2013 at 06:15 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Removing negative value after truncating

    Quote Originally Posted by dojob View Post
    Issit should be done by hardcoding if (value < -0.00000001, Math.abs(value)?
    Think carefully: what would that logic do?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default 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));
            }
        }
    kind regards,

    Jos
    dojob likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Removing negative value after truncating

    Quote Originally Posted by DarrylBurke View Post
    Think carefully: what would that logic do?

    db
    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.

  5. #5
    dojob is offline Member
    Join Date
    Jul 2013
    Posts
    52
    Rep Power
    0

    Default Re: Removing negative value after truncating

    Quote Originally Posted by JosAH View Post
    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));
            }
        }
    kind regards,

    Jos
    Brilliant. A simple trick to implement the property of sin value where it is periodic over 360 degree. Thanks.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default Re: Removing negative value after truncating

    Quote Originally Posted by dojob View Post
    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.
    Yep, the IEEE/754 format allows two different zeros: +0 and -0 and both compare equal to 0 but their String representation shows the difference.

    kind regards,

    Jos
    Last edited by JosAH; 08-04-2013 at 06:51 PM. Reason: wrong IEEE number ..
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Removing negative value after truncating

    Yes, but the logic in #1 converts all values less than -0.00000001 to their absolute values.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Need help with truncating decimals?
    By akeni in forum New To Java
    Replies: 3
    Last Post: 10-05-2011, 01:31 AM
  2. Mod of Negative Numbers
    By Venny in forum New To Java
    Replies: 7
    Last Post: 01-28-2011, 06:32 AM
  3. Help with negative numbers
    By cela0811 in forum New To Java
    Replies: 2
    Last Post: 01-04-2011, 06:04 PM
  4. Negative elements
    By swikar.java in forum New To Java
    Replies: 6
    Last Post: 12-15-2008, 05:48 AM
  5. Truncating decimal numbers in the output
    By gbade in forum New To Java
    Replies: 2
    Last Post: 11-21-2008, 07:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •