Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Double & Float primitive types

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Double & Float primitive types

    Hi guys,

    I have a quick question: What is the range the variable type "double" and "float" can hold, in terms of 2?, that is 2 tothepower of "x". Also the cardinality would be greatly appreciated.

    Thanks


  2. #2

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Double & Float primitive types

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Double & Float primitive types

    This are all good links, but they don't answer my question. I need to know the binary representation of a double type number inside the memory. For example an integer value takes space like this inside the 4 bits from the last to the first filling the 1 and 0 until the number is described. But what happens to float and double numbers? This question goes hand in hand with its range in terms of [ -2^x , 2^x + Y] or something similar

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Double & Float primitive types

    Huh? How did the links not answer that? And why do you think you need to know this?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Double & Float primitive types

    The binary representation of Java floats (or single precision floating point numbers) and Java doubles (or double precision floating point numbers) is very clearly presented in the first two Wikipedia links I posted. The two links also provide very clear formulas/methods for converting between the computer binary representation and a human readable format.

    IEEE 754-2008 - Wikipedia, the free encyclopedia provides a table of how many binary digits of precision you can expect from both of those two data types (float = binary32, double = binary64), as well as the range for the exponents.

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Double & Float primitive types

    This sure does smell like a homework assignment.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Aug 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Double & Float primitive types

    Actually no is not for homework. Yes I read the links which have a very pretty diagram showing the binary representation. I just needed a layman's terms explanation of how it works but thanks for judging <3.

    If you still are feeling friendly could you answer how to properly subtract doubles so that instead of 0.129999999999999 I get 0.13 when subtracting 20.37 from 20.50. Both values are stored in a double type instance variable.

    -Tried BigDecimal and it just makes it worse. Result: same with more digits.

    keeping it friendly,
    -zeek

  9. #9
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Double & Float primitive types

    You can't subtract doubles with BigDecimal (another edit: yes you can, but not the way you think you can). You can subtract BigDecimals with BigDecimals - that'll work. If you're constructing BigDecimals with a double value, your BigDecimal will already be 'wrong' - garbage in, garbage out and all that.

    edit: the API doc for the BigDecimal(double) constructor explains this very clearly I've just noticed.
    Last edited by Sean4u; September 11th, 2011 at 03:01 PM.

  10. #10
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Double & Float primitive types

    0.13 cannot be represent exactly in IEEE 754-2008 binary notation (for any finite mantissa bit length). This is one of the major implications of the way floating point numbers are represented why they are not used to for exact calculations (such as calculations involving money).

    For an online IEEE 754 converter:

    IEEE 754 Converter

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Double & Float primitive types

    Google "what every computer scientist should know about floating-point arithmetic" for a popular article that explains what you aren't understanding.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Tutorial: Primitive Data Types
    By newbie in forum Java Programming Tutorials
    Replies: 3
    Last Post: July 5th, 2012, 11:56 PM
  2. issues with a float
    By 2k. in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 30th, 2011, 12:03 PM
  3. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  4. Fitting a large primitive into a small reference variable
    By Phobia in forum Java Theory & Questions
    Replies: 15
    Last Post: October 23rd, 2009, 03:10 PM
  5. How to convert float and double data types to binary?
    By rosh72851 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 7th, 2008, 10:45 PM

Tags for this Thread