Borrar filtros
Borrar filtros

Operation with large and small numbers

8 visualizaciones (últimos 30 días)
Franklin
Franklin el 5 de En. de 2023
Comentada: Image Analyst el 6 de En. de 2023
1^4 + 1^4 + 23512^4 - 23512^4 = 2
but
Matlab says
1^4 + 1^4 + 23512^4 - 23512^4 = 0.
  1 comentario
Franklin
Franklin el 5 de En. de 2023
Please what is the quickest way to solve this problem? I need help with variable precision integer arithmetic.

Iniciar sesión para comentar.

Respuestas (2)

VBBV
VBBV el 5 de En. de 2023
(1^4 + 1^4) + (23512^4 - 23512^4)
  2 comentarios
VBBV
VBBV el 5 de En. de 2023
Editada: VBBV el 5 de En. de 2023
Add operation is done first on numbers and then subtracts. To get desired result use () which shows precedence in operator actions on numbers
Franklin
Franklin el 6 de En. de 2023
Thank you.
My numbers are variable. The order in wich they are grouped in parenthesis is important. (1^4 + 23512^4) + (1^4 - 23512^4) = 0.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 5 de En. de 2023
It's called truncation error. You should have learned about it in your linear algebra or numerical analysis course.
Basically it's caused by adding a gigantic number to a small number (2). There are not enough digits in 64 bits to accurately represent the 2 in the ones column of the number. The number if basically truncated and the 2 and perhaps even some other digits (like the 10's place) are lost.
To fix, use parentheses:
result = (1^4 + 1^4) + (23512^4 - 23512^4)
The first term is 2 and the second term is 0. Those numbers are in the same range and can be added with no truncation.
  2 comentarios
Franklin
Franklin el 6 de En. de 2023
Thank you.
My numbers are variable. The order in wich they are grouped in parenthesis is important. (1^4 + 23512^4) + (1^4 - 23512^4) = 0.
Image Analyst
Image Analyst el 6 de En. de 2023
See the FAQ:
You're still bound by truncation error.
Why is the order important, and why do you have such enormous numbers added to such tiny numbers?
You might have some luck with the symbolic toolbox, but I don't have that so can't advise you on that. Call tech support about that.

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by