Rounding error when dividing

조회 수: 13 (최근 30일)
ejk
ejk 2020년 6월 2일
댓글: ejk 2020년 6월 2일
I realized when dividng two small numbers, there is a rounding error that occurs.
For instance, if I do a series of calculations that eventually leave me with the numbers:
a = 5.669896651707230e-10
b = 1.417474162926808e-10
If I do a/b in the command window, the result will be ans = 4
Why doesn't MATLAB give me the decimal places?
I've tried both typecasting to double before dividing, and multiplying a and b by 1e10 before dividing, but all yield the same results, with the answer being an integer.
The numbers I am manipulating appear to have many digits after the decimal if I examine the workspace.
I manipulate the variables directly from the workspace, and do not input the numbers numerically (if you input the variables as stated above and do the division, the rounding error will not occur, but there will be a rounding error when pulling these values from the workspace).
How could I fix this and get the precision I need?
I've appended the actual code that this example pertains to below:
h = logspace(0,-25,100);
% Initialization
fxch = zeros(2,length(h));
dfdx = zeros(2,length(h));
% Operations
% Fun is just a function with two inputs
fxch(1,:) = Fun(-8+1i*h,-8);
dfdx(1,:) = imag(fxch(1,:))./h;
% The problem is that dfdx returns a mix of floating point numbers and integers
% where I need all elements of dfdx to be floats.

답변 (2개)

David Hill
David Hill 2020년 6월 2일
  댓글 수: 1
ejk
ejk 2020년 6월 2일
편집: ejk 2020년 6월 2일
That still gives me ans = 4. I'm looking to get as many decimal places as possible.

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


madhan ravi
madhan ravi 2020년 6월 2일
sprintf("%.55f",a/b)
  댓글 수: 5
madhan ravi
madhan ravi 2020년 6월 2일
upload the variables a and b as .mat file.
ejk
ejk 2020년 6월 2일
Good idea. I've attached them here.

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

카테고리

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