Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am in a computer science class (11th) for hw i have to do some code following a picture
--------------------Configuration: --------------------
C:\Users\I_SLAY_NOOBS\Desktop\Variables.java:26: error: possible loss of precision
float floatOne = 58.5678;
^
required: float
found: double
1 error
Process completed.
You assign a double which is more precise than a float to a variable declared as float. Float values end with an f in Java. Plain floating point numbers are automatically considered to be doubles...
Either do:
float f = 58.5678f;
or:
double d = 58.5678;
If you truly want a float (single precision IEEE754), you can use:
float floatOne = 58.5678f;
Me, I'd simply go with the double type everywhere since it provides more precision than float:
double doubleOne = 58.5678;
The default for floating point constants in Java (and C and C++, among others) is double precision and you're trying to shoehorn that into a single-precision variable, hence the message. Appending f to a floating point constant tells the compiler that you want it to be single precision.
Unless you have vast arrays of them (and limited space to store them), doubles are generally preferred.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
when i try to get the longValueExact() :
BigDecimal bigDecimal = new BigDecimal(432.900).divide(new BigDecimal(1), 2, RoundingMode.FLOOR);
System.out.println(bigDecimal.longValueExact());
Exception in thread "main" java.lang.ArithmeticException: Rounding necessary
at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4151)
at java.math.BigDecimal.needIncrement(BigDecimal.java:4207)
at java.math.BigDecimal.divideAndRound(BigDecimal.java:4115)
at java.math.BigDecimal.setScale(BigDecimal.java:2455)
at java.math.BigDecimal.longValueExact(BigDecimal.java:3093)
at com.tessi.bmd.specific.actil.utils.ActilUtils.main(ActilUtils.java:1281)
new BigDecimal(432.900)
This is a bad idea. 432.900 is a double literal and is therefore highly unlikely to actually represent 432.900. You're using BigDecimal, so presumably you know that there are only at most 2^64 numbers in existence that are exactly representable by a double. 432.900 is not one of them. Do not use this constructor - it has warnings all over it. Use new BigDecimal("432.9").
.divide(new BigDecimal(1),
Okay, divide by 1, not going to do anything. Also, use BigDecimal.ONE for this.
The value is still 432.899999999999434 or whatnot.
System.out.println(bigDecimal.longValueExact());
Of course that doesn't work - a long value can only hold integral values, and 432.9 (or something close to that) isn't.
Are you perhaps thinking that 432.900 is just a way of writing 432900 that is more readable to humans from certain locales where . is used as thousands separator?
. is the decimals separator. 432.900 is a double literal that represents the nearest representable double to the number 432 + 9/10ths. If that's your intend, remove the . - if you want to create some horizontal space for the yes, use _ which is legal in number literals and meaningless.
If that's not your problem and you really want 432.9 as an exact long - I guess, go back to square one and start learning java. Soon (as in, within a day or two, no doubt) you'll hit the part of the tutorial that explains the primitive data types. Pay extra attention to this section.
It is the normal behavior because the value you want to display as a long has decimal part (432.89, not 432.90 due to instatiating BigDecimal from a double) so is not an interger number. From the javadoc:
Converts this BigDecimal to a long, checking for lost information. If
this BigDecimal has a nonzero fractional part or is out of the
possible range for a long result then an ArithmeticException is
thrown.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Hi if I want to define double literal I wrote:
(1) double a = 12.0;
but when I define double without something after decimal point, for example
(2) double b=12;
is it more acceptable to use
(3) double c=12d;
instead?
From what I know (1) is the best way, If I can ask you what is the next better way to declare double literals which will be your choice and why?
From the Java language Specification:
A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.
The floating point types (float and double) can also be expressed
using E or e (for scientific notation), F or f (32-bit float literal)
and D or d (64-bit double literal; this is the default and by convention is omitted).
double d1 = 123.4;
// same value as d1, but in scientific notation
double d2 = 1.234e2;
float f1 = 123.4f;
So adding D or d is the same thing as omitting as you already know, but I would say that it is better to follow the convention and omit it.
It doesn't really matter. There is no difference whatsoever. The default type is double. So unless you place an f (if you want to treat the value as a float) the value is treated as double by default.It is the same thing as with integers. Default type is int. If you want a long you have to add an l otherwise everything is an int.
Compiler sugar. I use option 2 the most, but if i want to enforce calculation result i use option 3.
int intval = ...
double intdev = 1 / intval;
double doubledev = 1d / intval;
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
If we use float data type in Java we have to add f at the end of floating point literal as Java assumes it to be double data type and gives an error why not do the same for short and byte as both have lower ranges than int.
You can't have this thing with a byte for example because a byte can always be treated the same. It is allways a byte. But real numbers can only be represented as approximations. The difference between double and float is that double uses 64 bits and float 32. I.e. float is of less percision.
It is a similar thing to the int and long for integers. The default type for integers is int. Similarly the default type for real numbers is double.
Now if you want to use a float precision instead. You need to somehow let the compiler know.
That is precisely why the f is there. To tell the compiler how to treat the value.
Basically, when you do this float x = 0.1f you implicitly cast the 0.1 literal to a float.
This statement is equal to float x = (float) 0.1
Now lets try something:
float x = 0.1f;
double y = 0.1;
System.out.println(x == y) will give you false. Why? Lets see the first 20 digits after the decimal point:
0.1f --> 1000000014901161200
0.1 --> 1000000000000000055
I hope this helps.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm not sure if this question has been answered(couldn't find it when I did a google search).
I saw http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html that the math class square root method returns a double. I experimented with it in eclipse with some ints that have whole square roots - 4, 9 and found that the square root with those always returned a floating point value with one decimal - 4.0, 9.0. I was curious as to why it even returned that extra decimal? I thought that ints could be considered as double values too. To me returning just 1 makes more sense cause you conserve more memory(i guess more memory is needed to store that extra decimal point) I even tested it out
public static double control(){
return 1;
}
and saw it was valid to just return 1.
[I] found that [Math.sqrt(x) where x is a perfect square] always returned a floating point value with one decimal.
You are mistaking a particular printed representation of a double value with the value itself. A double does not have a decimal point. A double is a bit pattern that represents a particular real number (4 for example).
Decimal points only appear in a particuular decimal representation of real numbers. If I write "0.25", that obviously has a decimal point. If I write "1/4", there is no decimal point. But those are just two different representations of the same real number. So is the particular bit pattern that represents the double value returned by the Java expression, 1.0/4.0.
I don't know why Double.toString(4) returns the string, "4.0" instead of returning "4", but I'm guessing that somebody wanted to make it consistent with numeric literals in the Java language. When a "4" appears in your program, that's an int literal, and when "4.0" appears in your program, that's a double literal.
That method returns a double. It's going to display as a double because of the return type the method is set to. There is a solution to this here on stackoverflow to return the in if it's say 4.0 and show the double if it isn't. Solution on stackoverflow
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Can someone explain to me the difference between Float and float in java? Manythanks.
Float is an object; float is a primitive. Same relationship as Integer and int, Double and double, Long and long.
float can be converted to Float by autoboxing, e.g.
float f=1.0f;
Float floatObject = f;
or explicitly
Float floatObject = new Float(f);
Initially primitives were retained alongside the object versions for speed. Autoboxing/unboxing was added with java 5 to facilitate conversion.
Float is a class which wraps the primitive float. In newer versions of Java, a feature called autoboxing makes it hard to tell that they are different but generally speaking, use float when you using the number to do calculations and Float when you need to store it in Object collections.