Results 1 to 5 of 5
  1. #1
    jdodle is offline Member
    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Default Math.abs possibly returning a negative number

    Hi,

    the following code gives me an error saying that "n must be positive" but the abs function should only return positive numbers so why do I get this error?

    Java Code:
    y1 = 42 - Math.abs(6*(i-7));
    y2 = 21 - Math.abs(3*(i-7));
    			
    int range = Math.abs(y1 - y2);
    			
    dirt = land.nextInt(range) + y2;

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,016
    Rep Power
    6

    Default Re: Math.abs possibly returning a negative number

    Not enough information provided. You need to provide an Short, Self Contained, Correct Example and the error message. Otherwise, we are just guessing.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jdodle is offline Member
    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Default Re: Math.abs possibly returning a negative number

    Hi,

    The code I posted is the only code that pertains to the problem. It is part of a larger method that is used in the main method. here is the error it gives me:

    Exception in thread "main" java.lang.IllegalArgumentException: n must be positive
    at java.util.Random.nextInt(Random.java:250)
    at mapGenerator.island(mapGenerator.java:39)
    at mapGenerator.main(mapGenerator.java:15)

    I know what the error is saying (the random function can not take a negative value as an argument). the problem I have is that according to my code the argument should never be negative. so why does it tell me that it is getting a negative argument? when I assign the variable range as the value of Math.abs(-6) it runs just fine. but when it is Math.abs(y1 - y2) it gives me the error.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,016
    Rep Power
    6

    Default Re: Math.abs possibly returning a negative number

    No you still need to provide more code. What is the value of i? We certainly don't know. Perhaps range is zero. Did you print it out?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    jdodle is offline Member
    Join Date
    Oct 2013
    Posts
    11
    Rep Power
    0

    Default Re: Math.abs possibly returning a negative number

    Ok Im sorry for that, I now understand what you mean by more information. Luckily the problem was that the value was zero at one point, I was thinking that zero was a positive number. Thank you for your help.

Similar Threads

  1. Math.sqRt and negative numbers
    By SkoodlyWah in forum New To Java
    Replies: 4
    Last Post: 07-19-2013, 11:07 AM
  2. Replies: 1
    Last Post: 10-24-2012, 01:21 PM
  3. reversing number cant run negative number
    By irnie1994 in forum JCreator
    Replies: 2
    Last Post: 10-30-2011, 06:49 PM
  4. Find the First Negative Number of an Array
    By Cod in forum New To Java
    Replies: 12
    Last Post: 02-20-2011, 06:59 PM
  5. [SOLVED] Array of first negative number
    By random0munky in forum New To Java
    Replies: 9
    Last Post: 12-09-2008, 12:17 AM

Posting Permissions

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