That is not a problem .It is the output. 1e-7 refers to 1x(10^-7) or one multiplied by ten power minus seven .
That is not a problem .It is the output. 1e-7 refers to 1x(10^-7) or one multiplied by ten power minus seven .
Like the others have said, that is just scientific notation. If, however, you want to get all those decimal places instead of scientific notation. You can use System.out.printf("%f%n", d); where the %f means a float (double in your case) and the %n just means a new line.
Take a look at the java docs for more detail
Add a capital L to the end:
long value = 9223372036854775807L;
Otherwise, the compiler will try to parse the literal as an int, hence the error message
I don't know why it is referring to the long data type as an int
It is not. You should learn to trust compiler messages (especially when they are from sane, modern compilers and not ancient compilers that tended to have bad error messages). While the language that they speak might be hard to decipher at times, they are not usually lying to you.
Let's look at it again:
The literal of int 9223372036854775807 is out of range.
Note, that it doesn't mention your variable testLong or the type long anywhere, so the problem is not about the initialization. It seems to occur at some other point.
Now lets investigate some of the parts of the message:
inttells us that he wants to treat something as anintvalue (which is not what you wanted!)- "out of range" is pretty clear: something is not within the expected range (probably that of
int) - "The literal": now that's interesting: what is a literal?
I'll leave the cozy list to talk about literals for a moment: literals are places where you have some value in your code. There are String literals, int literals, class literals and so on. Every time you mention a value explicitly in your code, it's a literal.
So it's not actually nagging you about the variable declaration, but the number itself, the value is what it's nagging you about.
You can easily verify this by using the same literal in a context where a long and an int are equally acceptable:
System.out.println(9223372036854775807);
PrintStream.println can take either an int or a long (or pretty much anything else). So that code should be fine, right?
No. Well, maybe it should be, but according to the rules it is not fine.
The problem is that "some digits" is defined to be an int literal and therefore must be in the range defined by int.
If you want to write a long literal, then you must make that explicit by appending the L (or lower case l, but I highly suggest you always use the upper-case variant, because it's much easier to read and harder to mistake for a 1).
Note that a similar problem occurs with float (postfix F/f) and double (postfix D/d).
Side note: you'll realize that there are no byte or short literals and you can still assign values (usually int literals) to byte and short variables: that's possible due to special rules in § 5.2 about conversions in an Assignment Contexts: they allow assignment of constant expressions of a larger type to byte, short, char or int if the values are within the types range.
System.out.println(mWorldCounterBox.getCurrentAlivePopulationPercent()); //Output: 1.0544652507298142E-7
means mWorldCounterBox.getCurrentAlivePopulationPercent() is 1.0544652507298142 * ((10)^-7)
which is less then 0.1
SO After
EndScore += 20000.0; //1.0000105446525074+ 20000.0
EndScore becomes 20001.000010544652
Condition satisfying both the times.
If value is less than 0.1 its also less than 97.0
You put your conditions wrong i guess
mWorldCounterBox.getCurrentAlivePopulationPercent() is less than 0.1.
If the first condition satisfies
if(mWorldCounterBox.getCurrentAlivePopulationPercent() < 97.0) //mWorldCounterBox.getCurrentAlivePopulationPercent() <97.0 true
its always comes in to second condition also
if(mWorldCounterBox.getCurrentAlivePopulationPercent() < 0.1) //mWorldCounterBox.getCurrentAlivePopulationPercent() <0.1 true
side Note:
Please follow java naming conventions.Ex: EndScore should be endScore
Java's Primitive Data Types
boolean: 1-bit. May take on the values true and false only.
byte: 1 signed byte (two's complement). Covers values from -128 to 127.
short: 2 bytes, signed (two's complement), -32,768 to 32,767
int: 4 bytes, signed (two's complement). -2,147,483,648 to 2,147,483,647.
long: 8 bytes signed (two's complement). Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
float: 4 bytes, IEEE 754. Covers a range from 1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative).
double: 8 bytes IEEE 754. Covers a range from 4.94065645841246544e-324d to 1.79769313486231570e+308d (positive or negative).
char: 2 bytes, unsigned, Unicode, 0 to 65,535
Java's Double class has members containing the Min and Max value for the type.
2^-1074 <= x <= (2-2^-52)·2^1023 // where x is the double.
Check out the Min_VALUE and MAX_VALUE static final members of Double.
(some)People will suggest against using floating point types for things where accuracy and precision are critical because rounding errors can throw off calculations by measurable (small) amounts.
When the result crosses the maximum values of an int then it is overflowed ie, integer overflow. You may better want to use long instead of int.
You may be interested to read: Integer overflow and underflow in Java.
Arithmetic integer operations are performed in 32-bit precision. When the resultant value of an operation is larger than 32 bits (the maximum size an int variable can hold) then the low 32 bits only taken into consideration and the high order bits are discarded. When the MSB (most significant bit) is 1 then the value is treated as negative.
When the result of an int operation (such as multiplication) is higher than the max int value, it overflows (i.e. doesn't fit within the 32 bits of an int variable), which means the value assigned to the int variable is incorrect. You have no reason to expect it to return the max int value if the correct result is higher.
If you want a correct result, use longs.
You can throw IllegalArgumentException
Java Language Specification, regarding Integer Operations says:
The integer operators do not indicate overflow or underflow in any way.
An integer operator can throw an exception (§11) for the following reasons:
Any integer operator can throw a
NullPointerExceptionif unboxing conversion (§5.1.8) of a null reference is required.The integer divide operator / (§15.17.2) and the integer remainder operator % (§15.17.3) can throw an
ArithmeticExceptionif the right-hand operand is zero.The increment and decrement operators ++ (§15.14.2, §15.15.1) and -- (§15.14.3, §15.15.2) can throw an
OutOfMemoryErrorif boxing conversion (§5.1.7) is required and there is not sufficient memory available to perform the conversion.
I m not a fan of "exception driven architecture", ;)
Implement a check if the integer is over 100 and return an error, status code, etc as feedback for user/system etc.
Yes, I believe you should throw an exception, to help your fellow programmers notice the error at compile-time, by also adding throws ArgumentOutOfBoundsException in your method declaration.
That is, unless you use imaginary numbers in your project, where -1 is a perfectly valid argument for your square root method. :)
If you can code your method or its signature in such a way that the exception condition cannot happen, doing so is arguably the best approach.
For example, with the particular example you bring up, does your language have an unsigned integer data type with an appropriate range? If so, just use that one rather than a corresponding signed integer type and throwing an exception for any value passed in that is less than 0. In that case, a negative value cannot be used in that position in a well-formed program; the compiler will catch the mistake long before runtime, and at the very least issue a warning (for signed/unsigned type use mismatch), unless you go out of your way to explicitly bypass that safety net; and if you're forcing a signed variable into a function whose method signature says unsigned integer only without making sure that the value is within an acceptable range for that function, then I'd argue that the programmer doing so deserves to get an exception thrown in their face.
As another example, if only a reasonably small set of values is valid as input, maybe those can be expressed as an enumeration with a specific set of values rather than using a plain integer value to encode each possible value? Even if under the hood the enum collapses to an int, using enumerations protects from programmer mistakes.
If you cannot write the method signature in such a way that the exception condition cannot possibly happen in a well-formed program, then I would argue that this is exactly what ArgumentOutOfRangeException, IllegalArgumentException and similar exception types are intended for. This can also trivially be worked into a TDD workflow. Just make sure to make it clear what the error is; particularly if you don't have easy access to the source code, getting a non-specific ArgumentOutOfRangeException thrown back at you can be greatly frustrating. Note the documentation for these exception types:
ArgumentOutOfRangeException (.NET): "The exception that is thrown when the value of an argument is outside the allowable range of values as defined by the invoked method." (as seen here)
IllegalArgumentException (Java): "Thrown to indicate that a method has been passed an illegal or inappropriate argument." (as seen here)
Writing the method to make the exception condition impossible to begin with will help even more than throwing an exception at runtime, but we must acknowledge that sometimes this is just not possible in practice. You'll still need to check invariants not directly related to the range of a single parameter value at runtime, though; for example, if x + y > 20 is an error, but x and y each can be anything <= 20, I don't think there's an easy way to express that in the method signature.
Maybe something like (for numbers 1-20 and 50-100):
Random r = new Random();
double randomValue = r.nextDouble()*70;
if(randomValue>20) randomValue+=30;
It is not resource expensive and easy to understand.
You can try somethink like this :
Random rnd = new Random();
double x=0;
do{
x = rnd.nextDouble()*100;
}while(x>20 && x<50 );
System.out.println(x);
}
You generate a random double ( need multiply by 100 because generate double return value between 0 and 1 ) and loop while result >20 and <50
I’ve tried looking for definitive answers but the only difference I see seems to be based on the precision of the numbers?
Which data type is larger and which one is smaller?
To generate a random value between rangeMin and rangeMax:
Random r = new Random();
double randomValue = rangeMin + (rangeMax - rangeMin) * r.nextDouble();
This question was asked before Java 7 release but now, there is another possible way using Java 7 (and above) API:
double random = ThreadLocalRandom.current().nextDouble(min, max);
nextDouble will return a pseudorandom double value between the minimum (inclusive) and the maximum (exclusive). The bounds are not necessarily int, and can be double.