# Float literals.

Shivani Chandna
Ranch Hand
Posts: 380
Java 1.4//
Why am I getting error on line 3 and not one line 1 and 2?

Thanks,
--

Shivani,
[ September 25, 2005: Message edited by: Shivani Chandna ]

Barry Gaunt
Ranch Hand
Posts: 7729

Barry Gaunt
Ranch Hand
Posts: 7729
On the line 3 you have an invalid digit. You have the letter "l" (el) and not a "1" (one)

Shivani Chandna
Ranch Hand
Posts: 380
Sad - :-/ what an error ! :roll:
I looked up - on EditPlus both l and 1 were looking alike .

Jayant Raj
Ranch Hand
Posts: 42
Can you please explain the type conversions taking place here?
According to my understanding,

will throw a compile time error. If we dont append 45.64e with f, it will implicitly be a double.Thus the right hand expression will be a double which cannot be assigned to a float.

A Kumar
Ranch Hand
Posts: 980
Hi,

There is no compilation error...As you said

45.64e is double and hence the whole expression would be implicitly a double expression...

After the evaluation it is converted back to float..

here 45.64e is within the range of float and hence there is no compilation error.

But if the evaluated value exceeds the range of float it needs to be

explicitly casted else a compilation error..

An interesting thing in your example...

If the literal is defined as 1.0f then error occurs ..Why???

Regards
[ September 26, 2005: Message edited by: A Kumar ]

Joji Doi
Greenhorn
Posts: 3
>>If the literal is defined as 1.0f then error occurs ..Why???
Look at the following as a float literal rather than literals.

float f1 =45.64e-1f;

The above statement is NOT 45.64e - 1f as minus arithmetic.
45.64e-1 is double literal. The exponent cannot be floating point.

Thus the following is equivalent:
float f1 =(float)45.64e-1;

Cheers!

A Kumar
Ranch Hand
Posts: 980
Hi,

That means my explanation above is not in tune with the above code...

I overlooked that

Regards

Jenny Thompson
Greenhorn
Posts: 1
Hi ,
The error is nothing bcoz of logical thing i belive..
when i compiled ur code i got the following error message..

Test.java:5: ';' expected
float f3 =45.l4e-1;
^
1 error

instead of numerical value one(1)in ur code lowercase of L is present..
it caused the problem..if u change it to one it s compiling nicely...
no error occurs.

Akhilesh Trivedi
Ranch Hand
Posts: 1608
Guys, before you answer, don't just read the question alone, do also read the opinions and discussion that follows under it. I guess the author has already discovered the fault.

Originally posted by Shivani Chandna:
Sad - :-/ what an error ! :roll:
I looked up - on EditPlus both l and 1 were looking alike .

[ September 27, 2005: Message edited by: Akhil Trivedi ]