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

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.

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?

Exactly the same way as any other computing language does.sasha vakulin wrote:. . . 1. How java convert this values into binary?

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 method2. 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?

`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.

Campbell Ritchie wrote:Welcome to the Ranch :)

Exactly the same way as any other computing language does.sasha vakulin wrote:. . . 1. How java convert this values into 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)

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)?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

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.

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?

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?

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?

It's an oldie but Goldie*

* pun intended—see the author's name

*Practice only makes habit, only perfect practice makes perfect.
Practice mindfully by doing the right things and doing things right.*— Junilu

[How to Ask Questions] [How to Answer Questions]

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

- 1

In the case of how thesasha vakulin wrote:. . . Java rounding this binary (last bit) or java cuts this binary after 64 bit without rounding?

`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:-

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 expectedyou don't need to know that!

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.

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?

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.

Campbell Ritchie wrote:

In the case of how thesasha vakulin wrote:. . . Java rounding this binary (last bit) or java cuts this binary after 64 bit without rounding?

doubleis 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:-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 expectedyou don't need to know that!

At what moment of convertation is used strictfp?

- 1

- 1

I am by no means convinced you will find assembler any easier to understand. You can view the bytecode with the javap tool. Trysasha vakulin wrote:. . . I think I have to search info about it in assembler.

`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!

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?

Consider Paul's rocket mass heater. |