Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile error when I try to use float, works if I change it to double..

 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Here's the first source code:


And here's the second source code (the one which contains the main method):



I've commented, so just read the comments.
The problem is as you can see, that it runs just fine if I declare "vikt" as a double, but I can't declare it as a float.. (which is weird, since they're the same except double takes more space in the memory and you can define more precise numbers with it, maybe there are even more details, but please, let's stick to the basics for now, which is explaining why double works and float doesn't :P )
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Staying with the basics:
First, you should double check the tutorial. I think this one has most of what you need: datatypes.

Second: The default data type for any number with a decimal point is a double. So unless you define the constant as a float, you will get a double instead of a float. Like other primitives you can not automatically assign from a 'wider' data type (takes more bits) to a 'narrower' one, because of the loss of data. You could use float, though, if you called the method with aket.setVikt(1.3f);
 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm, ok.
But how is the value 1.3 too wide for float, doesn't 1.3 fit into 32 bits?

And what is aket.* ?

Would it work if I change this: to this?

I think I'll just accept the double for now, but I'm still curious.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ethan Bauer wrote:
But how is the value 1.3 too wide for float, doesn't 1.3 fit into 32 bits?

it's not and it does.

however, when you simply write a literal value with a decimal point, Java assumes you have a double, not a float. And no double, regardless of the value, will fit into a float. Java thinks you have the value 1.30000000000000000000000000000000000000001213 (or something like that). you are trying to cram that into a variable that will only hold the value 1.30000000000000983 (or something like that).

So, tell java you really have a float:

paket.setVikt(1.3f);
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ethan Bauer wrote:hmm, ok.
But how is the value 1.3 too wide for float, doesn't 1.3 fit into 32 bits?

1.3 is a double, and a double is 64 bit. So no, it doesn't fit. There is actually a difference in the internal storage and precision between float and double (not so basic stuff) so even though the range seems like it should fit the reality is that the precision keeps it from doing so.

And what is aket.* ?

That was a mis-copy from your code. should have been paket.setVikt(...) not aket.setVikt(...)

Would it work if I change this: to this?

Should be rather painless to try, right? Why not give it a go and see?

I think I'll just accept the double for now, but I'm still curious.
 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha, alright, I'll try it next time and THEN follow up with a question.
Didn't know Java automatically thinks it's a double if you write down a number with decimals, now I know that I gotta mark it with "f" for floats, same as when you write longs, you mark it with L
Thanks for the help.
 
Stephan van Hulst
Bartender
Posts: 5334
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know your reason for using a float instead of a double, but you can just use the double if you don't have very specific reasons not to use a double.

It's like the integer datatypes as well. Java uses int by default, but there are also the smaller byte and short available, as well as the bigger long.
 
Ethan Bauer
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I usually use int for integers and double for floating point numbers, but I was just curious in this case and thought I'd try float..
 
Stephan van Hulst
Bartender
Posts: 5334
48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Experimenting is always good. I'm glad you figured it out
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic