A
A
AndreyIvanoff2011-06-05 22:52:27
Mathematics
AndreyIvanoff, 2011-06-05 22:52:27

Floating point calculations?

Hello Khabrovites.
The question may seem strange, but it leads me to severe bewilderment.
I will give the calculations performed in MatLab:
Command: sqrt(2) = 1.4142135623731 - 13 significant digits after the decimal point.
Command: sqrt(98) = 9.89949493661167 - 14 decimal places.
As far as I understand, there can be no more than 15 decimal places after the comma in the double type.
And if you honestly calculate these roots:
vpa(sqrt(2),16) = 1.414213562373095 - 15 significant digits.
vpa(sqrt(98),16) = 9.899494936611665 - 15 significant digits.
Note that rounding in the case of 13 and 14 characters is carried out according to all the rules and is correct.
However, I do not understand the reason why the sqrt function produces a different number of digits for different digits? What is the reason for this and what is the logic of such behavior? I observed the same with C compilers.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
B
burdakovd, 2011-06-06
@burdakovd

"As far as I understand, there can be no more than 15 decimal places after the comma in the double type." - where did you get it from?
Firstly, in floating-point arithmetic, there is no difference between “before the decimal point” and “after the decimal point” - everything is transferred to “after the decimal point”, even the “1” or “9” that you had in the integer part (this explains the case with sqrt(2) (the 15th digit was before the decimal point), and sqrt(98) is just the last digit =0 ).
Secondly, do not forget that arithmetic is binary, not decimal, so there will not always be exactly 15 characters, there may be more miracles. Well, IEEE 754.

A
Andrew, 2011-06-06
@OLS

In floating point formats, it is not the number of significant digits after the decimal point that is fixed, but the number of significant digits of everything in the record (moreover, binary significant digits). So it turns out that the larger the number to the left of the decimal point, the less precision after the decimal point (I mean the numbers are normalized).

H
hybridcattt, 2011-06-05
@hybridcattt

A different number of significant digits indicates that in the 1st case the last digit was rounded to 0.
In general, the number of digits after the decimal point is not exactly - 15-16, for float - 6-7. This is because the arithmetic is actually binary, not decimal.

N
nerudo, 2011-06-05
@nerudo

How did you get such results?
The sqrt() function does not print to the screen, it only evaluates the expression. And it must do so with as much precision as possible [for the data type].
Here I write:
> sqrt(2)
ans =
1.4142
And here:
> fprintf('%.16f\n',sqrt(2))
1.4142135623730951
Matlab 2010b

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question