• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading materials on floating point operations in Java?  RSS feed

 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Do you know any info (article, web pages, books and ...) about floating point operations on Java?

I want understand : how Java adding   two double values (on machine level  or binary level with rounding) and why it does not give correct answer?

What happening on deep level (step by step)?

Example:




After run:

7.5708
11.356200000000001

But it must have:

7.5708
11.3562
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sasha, welcome to the Ranch!

There are times when it can be useful to understand how floating point arithmetic works. However since you said

But it must have:

7.5708
11.3562


I would suggest this isn't one of those times. If you need to control decimal precision like that, then floating precision arithmetic is just going to get in your way. You would be better off to use BigDecimal objects instead.
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:Hi sasha, welcome to the Ranch!

There are times when it can be useful to understand how floating point arithmetic works. However since you said

But it must have:

7.5708
11.3562


I would suggest this isn't one of those times. If you need to control decimal precision like that, then floating precision arithmetic is just going to get in your way. You would be better off to use BigDecimal objects instead.


But I want to understand (step by step) how java summing two floating point value (double type).

1. How java convert this values into binary?

2. What type and perfomance of this binary (sign, mantis, exponent, 80 or 64 bit length)?

3. How java rounding this binary?

4. How java summing two binary value?

5.  How java rounding this binary sum?

6. How java convert binary sum to decimal sum?
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch :)
sasha vakulin wrote:. . . 1. How java convert this values into binary?
Exactly the same way as any other computing language does.
2. What type and perfomance of this binary (sign, mantis, exponent, 80 or 64 bit length)?
3. How java rounding this binary?
4. How java summing two binary value?
5.  How java rounding this binary sum?
6. How java convert binary sum to decimal sum?
You can start to find your answers in the Java® Language Specification, and look somewhere like Wikipedia for the other details. The answer to question 2 is implementation‑dependent and dependent on whether you have or haven't marked the method strictfp. Java® does not convert the binary numbers to decimal, but can display them as decimal numbers, and that process differs from method to method. If you use System.out.println(d) you can find more details here.
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch :)
sasha vakulin wrote:. . . 1. How java convert this values into binary?
Exactly the same way as any other computing language does.

7.5708 = 0 10000000001 1110010010000111111111001011100100100011101000101010 (Inexact, 1+11+52 bit = 64 bit)


3.7854 = 0 10000000000 1110010010000111111111001011100100100011101000101010 (Inexact, 1+11+52 bit = 64 bit)

I'm right? This values 7.5708 and 3.7854   not exact  convert to binary, how java rounding this binary (last bit) or java cuts this binary (without rounding)?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12562
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in a nutshell, floating point numbers are ALL rounded off.  It has to do with the fact that numbers are really represented as the sum of powers of 2...including fractions like 1/2, 1/4, 1/8...There are limites to how precise they can be.
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Convert to binary are carried out in the FPU 80-bit (extended) representation?

I correctly convert decimal to binary?


 
Fred Kleinschmidt
Bartender
Posts: 571
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simple analogy. Write down the decimal representation of 1./3.
The answer: 0.3333333
Oops, not quite exact; try a few more digits: 0.3333333333333333333333333333333333333333333333333333333333333333
Oops, still not quite exact. Try again with 10,000,000,000 digits. Oops, STILL not quite exact.
The same thing occurs using binary representation of numbers: very few numbers can be represented exactly using a finite number of digits, in any base.
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:in a nutshell, floating point numbers are ALL rounded off.  It has to do with the fact that numbers are really represented as the sum of powers of 2...including fractions like 1/2, 1/4, 1/8...There are limites to how precise they can be.


Which floating point numbers (dec or binary) are ALL rounded off?

Java rounding this binary (last bit) or java cuts this binary after 64 bit without rounding?

This is Raw binary:

7.5708 = 0 10000000001 1110010010000111111111001011100100100011101000101010 (Inexact, 1+11+52 bit = 64 bit)
3.7854 = 0 10000000000 1110010010000111111111001011100100100011101000101010 (Inexact, 1+11+52 bit = 64 bit)

May be java covert decimal to binary:


7.5708 = 111.1001001000011111111100101110010010001110100010101
3.7854 = 11.11001001000011111111100101110010010001110100010101



Convert to binary are carried out in the FPU 80-bit (extended) representation?

I correctly convert decimal to binary?
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Here's a simple analogy. Write down the decimal representation of 1./3.
The answer: 0.3333333
Oops, not quite exact; try a few more digits: 0.3333333333333333333333333333333333333333333333333333333333333333
Oops, still not quite exact. Try again with 10,000,000,000 digits. Oops, STILL not quite exact.
The same thing occurs using binary representation of numbers: very few numbers can be represented exactly using a finite number of digits, in any base.


Java rounding this binary (last bit) or java cuts this binary after 64 bit without rounding?
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:Here's a simple analogy. Write down the decimal representation of 1./3.
The answer: 0.3333333
Oops, not quite exact; try a few more digits: 0.3333333333333333333333333333333333333333333333333333333333333333
Oops, still not quite exact. Try again with 10,000,000,000 digits. Oops, STILL not quite exact.
The same thing occurs using binary representation of numbers: very few numbers can be represented exactly using a finite number of digits, in any base.



Java has Round-Half-Even (Banker's Rounding), when convert decimal to binary?
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See this article: What Every Computer Scientist Should Know About Floating-Point Arithmetic

It's an oldie but Goldie*

* pun intended—see the author's name
 
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
sasha vakulin wrote:Convert to binary are carried out in the FPU 80-bit (extended) representation?

I correctly convert decimal to binary?


Java floats are in IEEE 754 single precision format. Java doubles are in IEEE 754 double precision format. And Java doubles with strictfp, are still in IEEE 754 double precision format, but any calculation's intermediate values are stored in extended precision format, provided that it is supported by the underlying hardware.  I guess that should be enough information for you to Google the actual bit representation...

Henry
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm finding it very hard to find anything online which is specifically about how Java converts decimal values (like in your original post) to floating point notation. (So the internet agrees with me when I say you don't need to know that!) But on the third page of links returned by my web search I found this page: Floating Point which, in its Q + A section, does talk about that kind of internal bit-fiddling.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sasha vakulin wrote:. . . Java rounding this binary (last bit) or java cuts this binary after 64 bit without rounding?
In the case of how the double is displayed, the details are in the link to Double.toString() which I provided yesterday. As Henry has said, Java® follows IEEE754 exactly, but any extension is hardware dependent; that is why the keyword strictfp was introduced in jdk1.2(?). Have a look at the IEEE754 standard, or the Wikipedia article about it, as well as the links other people have given. And remember what Paul C said:-
you don't need to know that!
The details might vary from chip to chip; I can remember when some Intel chips had their own interpretation of IEEE754, producing results completely different from what everybody expected
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:See this article: What Every Computer Scientist Should Know About Floating-Point Arithmetic

It's an oldie but Goldie*

* pun intended—see the author's name


This article is very hard.

This article don't teach about suming two double value step-by- step.

I think I have to search info about it in assembler.
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
sasha vakulin wrote:Convert to binary are carried out in the FPU 80-bit (extended) representation?

I correctly convert decimal to binary?


Java floats are in IEEE 754 single precision format. Java doubles are in IEEE 754 double precision format. And Java doubles with strictfp, are still in IEEE 754 double precision format, but any calculation's intermediate values are stored in extended precision format, provided that it is supported by the underlying hardware.  I guess that should be enough information for you to Google the actual bit representation...

Henry


At what moment of convertation is used strictfp?
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'm finding it very hard to find anything online which is specifically about how Java converts decimal values (like in your original post) to floating point notation. (So the internet agrees with me when I say you don't need to know that!) But on the third page of links returned by my web search I found this page: Floating Point which, in its Q + A section, does talk about that kind of internal bit-fiddling.


Thank you very much for your answer. But what part of the QA is talking about it.
 
sasha vakulin
Greenhorn
Posts: 16
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
sasha vakulin wrote:. . . Java rounding this binary (last bit) or java cuts this binary after 64 bit without rounding?
In the case of how the double is displayed, the details are in the link to Double.toString() which I provided yesterday. As Henry has said, Java® follows IEEE754 exactly, but any extension is hardware dependent; that is why the keyword strictfp was introduced in jdk1.2(?). Have a look at the IEEE754 standard, or the Wikipedia article about it, as well as the links other people have given. And remember what Paul C said:-
you don't need to know that!
The details might vary from chip to chip; I can remember when some Intel chips had their own interpretation of IEEE754, producing results completely different from what everybody expected


At what moment of convertation is used strictfp?
 
Tim Moores
Saloon Keeper
Posts: 4019
94
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you find the Goldberg article a bit too advanced, start here: http://mindprod.com/jgloss/floatingpoint.html
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sasha vakulin wrote:. . . I think I have to search info about it in assembler.
I am by no means convinced you will find assembler any easier to understand. You can view the bytecode with the javap tool. Try
javap -c MyClass
The exact details of the bit by bit arithmetic will be hidden in the depths of the IEEE754 standard and the chip, however, so I don't think bytecode or assembler will supply the information you want. To repeat what Paul C said:
you don't need to know that!
 
Paul Clapham
Sheriff
Posts: 22817
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sasha vakulin wrote:
Paul Clapham wrote:I'm finding it very hard to find anything online which is specifically about how Java converts decimal values (like in your original post) to floating point notation. (So the internet agrees with me when I say you don't need to know that!) But on the third page of links returned by my web search I found this page: Floating Point which, in its Q + A section, does talk about that kind of internal bit-fiddling.


Thank you very much for your answer. But what part of the QA is talking about it.


"A. Here is a decimal to IEEE converter. To get the IEEE 754 bit representation of a double variable x in Java, use Double.doubleToLongBits(x). According to The Code Project, to get the smallest double precision number greater than x (assuming x is positive and finite) is Double.longBitsToDouble(Double.doubleToLongBits(x) + 1). "

The second question (of many). That was what you asked, wasn't it?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!