Why is f placed after float values?

I don’t know why f or F are placed after float values ​​in Java or other languages? eg,

float fVariable = 12.3f; 

any functions other than indicating that this is a float value?

+53
java floating-point
Mar 17 '12 at 7:26
source share
6 answers

default 12.3 is double literal, therefore, to tell the compiler that it explicitly float uses f or f

+41
Mar 17 '12 at 7:28
source share
β€” -

Seeing that there are only so many ways to represent a number in your program, Java developers had to select and assign each form to the most common use case. For those forms that are selected by default, a suffix that indicates the exact type is optional.

  • For integer literals (int, long), the default value is int. For obvious reasons.
  • For floating point literals (float, double), the default value is double. Since using double potentially allows safer arithmetic to stored values.

So, when you enter 12 into your program, it is int literal, as opposed to 12L , which is long. And when you type 12.3 , it is a double literal, not 12.3F , which is a float.

So where is this relevant? First of all, when processing down-converting or narrowing conversions. Whenever you omit a long path to int or double-float, there is a possibility of data loss. Thus, the compiler will force you to indicate that you really want to narrow the conversion, signaling a compilation error for something like this:

 float f = 12.3; 

Since 12.3 is a double, you have to explicitly pass it to a float (basically, by subscribing to narrowing the conversion). Otherwise, you could indicate that the number is indeed a float using the correct suffix;

 float f = 12.3f; 

So to summarize too much, you need to specify a suffix for longs and floats - this is a compromise that the language developers have chosen to balance the need to specify what exactly such a number is, with the flexibility of converting numbers from one type of storage to another.

+36
Mar 17 '12 at 10:13
source share

float and double can provide approximate view values ​​for some values. e.g. 12.3 or 0.1

The difference is that the float is not so accurate (since it has less accuracy, because it is less)

eg.

 System.out.println("0.1f == 0.1 is " + (0.1f == 0.1)); System.out.println("0.1f is actually " + new BigDecimal(0.1f)); System.out.println("0.1 is actually " + new BigDecimal(0.1)); 

prints

 0.1f == 0.1 is false 0.1f is actually 0.100000001490116119384765625 0.1 is actually 0.1000000000000000055511151231257827021181583404541015625 

So 0.1 is the closest representation in double , and 0.1f is the closest representation in float

+19
Mar 17 2018-12-12T00:
source share

float fVariable = 12.3; It's good. but when you use only the float value (without any identifier) ​​in any expression that you need to tell the compiler, this value is equal to float, so we use the suffix "f" after the value. Example

float fl = 13f / 5f;

here 13 and 5 are floating point values.

+5
Mar 17 '12 at 9:20
source share

At compile time, all floating point numbers (decimal point numbers) double by default.

Therefore, if you do not want your number to double and just want it to be like a float, you must explicitly tell the compiler by adding f or F to the end of the literal constant.

+1
Nov 10 '14 at 10:51
source share

Float is a 32-bit 32-bit floating point IEEE 754 with double precision, and Double is a 64-bit floating point IEEE 754 with double precision. When you use a value with decimal points, and if you do not specify it as 0.23f (specifically float), java identifies it as double.

For decimal values, a double data type is usually the default choice chosen by java. Check it out

0
Mar 10 '14 at 5:30
source share



All Articles