How to reduce precision

조회 수: 1 (최근 30일)
F?rat
F?rat 2011년 12월 27일
Hello, I am using cross products to find an angle between two vectors (not using dot product because I am also using the cross product resultant vector) and in the limits, I get numbers such as e^-15 which I do not need. I need at most 4 digits after the point. Is there a code or simple way other than dividing with 0.001, rounding and mltiplying again with 0.001. Any help would be appreciated. F?rat

답변 (1개)

Walter Roberson
Walter Roberson 2011년 12월 28일
Multiplying by 10000, rounding, and dividing by 10000 is about the best possible in MATLAB.
Warning: Except in the cases that are integer multiples of exact powers of 2, such as 0.75 (3 * 2^(-2)), it is not possible in MATLAB to truncate floating point numbers to have exactly a certain number of decimal places. Floating point numbers are represented in binary, and none of 0.1 and 0.01 and 0.001 and 0.0001 have exact binary representations. You will end up with low-order bits set so as to come as close as possible to a multiple of 0.0001 in binary representation, but you will never get 0.0001 exactly.
Try
fprint('%.99g\n', 0.0001)
and you will see that the value is not exactly 0.0001
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 12월 28일
Note: the Symbolic Toolbox can do decimal arithmetic, as can the Fixed Point Toolbox.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by