• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

float vs int

 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should print
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
https://coderanch.com/t/391176/java/java/Floating-point-numbers-range
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
https://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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, if you round 1299.999999 to three sig fig you get 1200.
 
God is a comedian playing for an audience that is afraid to laugh - Voltair. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic