• Post Reply Bookmark Topic Watch Topic
  • New Topic

float vs int  RSS feed

 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
which one is wider ?


Is it mean float is wider than int becuase explicitly int value is being assigned to float variable without float casting ?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apart from whether you should use floats at all.
Yes, float is wider than int. You will find the details here.
You do realise that after your code, System.out.println(f); will not display 1234567890?
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should print
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalini Srivastav wrote:It should print
Should, maybe. Will print, No. Actually the code I posted won’t necessarily print the last .0
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The precision of a float is approximately 6 or 7 decimal digits - it cannot hold 10 decimal digits, so it won't print 1234567890.0.
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:The precision of a float is approximately 6 or 7 decimal digits - it cannot hold 10 decimal digits, so it won't print 1234567890.0.

But below post shows, it can hold more than 10 decimal digits i.e. 340,000,000,000,000,000,000,000,000,000,000,000,000
http://www.coderanch.com/t/391176/java/java/Floating-point-numbers-range
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shalini Srivastav wrote:
Jesper de Jong wrote:The precision of a float is approximately 6 or 7 decimal digits - it cannot hold 10 decimal digits, so it won't print 1234567890.0.

But below post shows, it can hold more than 10 decimal digits i.e. 340,000,000,000,000,000,000,000,000,000,000,000,000
http://www.coderanch.com/t/391176/java/java/Floating-point-numbers-range



Jesper is referring to significant digits. In your example, it has two significant digits.

Henry
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some people say that 0 is not a digit, so 3400000000000000... is actually two digits.
The example given prints (I think) 1.2345694e+9. After the 6, the figures shown are inaccurate.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Some people say that 0 is not a digit, so 3400000000000000... is actually two digits.
The example given prints (I think) 1.2345694e+9. After the 6, the figures shown are inaccurate.

Zero is DEFINITELY a digit.

Even if you are talking about SIGNIFICANT digits, zero CAN be:

10001

this has five significant digits. Zero doesn't HAVE to be significant, as your above example points out.

And this:

0.0000001

is seven significant digits (ok...maybe six...i'm not sure if you count the zero in the 'ones' place).
 
Shalini Srivastav
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:

And this:

0.0000001

is seven significant digits (ok...maybe six...i'm not sure if you count the zero in the 'ones' place).


It has only one significant digit.correct me if i am wrong.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leading zeros are insignificant. You are correct, 0.000001 has only one significant digit. Trailing zeros are significant: 0.000000100 has three significant digits, 1 and the two zeros after it.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A digit is significant if it resembles a value. So after the decimal point, all the digits listed are significant 0000001.

0.000001000 - here the 3 zeros followed by the 1 are insignificant.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:A digit is significant if it resembles a value. So after the decimal point, all the digits listed are significant 0000001.

0.000001000 - here the 3 zeros followed by the 1 are insignificant.


You have it backwards - http://en.wikipedia.org/wiki/Significant_figures

I had the tendency to think the same way but the trick I use to remember the correct way is to remember that it's counter-intuitive (just as it apparently is to you and others).
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok Junilu, I mistakenly thought the zeros between the decimal and the digit (1-9) has a significance. (i.e. - .001 signifies 1/1000 ).

And you are correct.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did say some people say 0 isn’t a digit. What people were saying about significant digits, however, is exactly what I learnt at school back in 1965. In 340000000000000000.... you have two significant digits., but 300004000000000000000... has six.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, technically when you see "1200" all you know is that there are at least two significant digits. You don't know whether any of the zeroes after them are significant or not, and it's possible that some of them are.

That's why we have scientific notations to make this sort of thing clear when it's necessary. Thus 1.2 * 10^3 has two significant digits but 1.20 * 10^3 has three, even though they appear to represent the same number.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, if you round 1299.999999 to three sig fig you get 1200.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!