But it must have:
7.5708
11.3562
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.
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 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.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 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
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.
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.
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.
The best ideas are the crazy ones. If you have a crazy idea and it works, it's really valuable.—Kent Beck
How to Ask Questions | How to Answer Questions | Format Your Code
sasha vakulin wrote:Convert to binary are carried out in the FPU 80-bit (extended) representation?
I correctly convert decimal to binary?
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:-sasha vakulin wrote:. . . Java rounding this binary (last bit) or java cuts this binary after 64 bit without rounding?
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
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
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.
Campbell Ritchie wrote:
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:-sasha vakulin wrote:. . . Java rounding this binary (last bit) or java cuts this binary after 64 bit without rounding?
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!
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.
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.
Consider Paul's rocket mass heater. |