Rounding error when dividing

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.

Antworten (2)

David Hill
David Hill am 2 Jun. 2020

0 Stimmen

round(a/b,12);

1 Kommentar

ejk
ejk am 2 Jun. 2020
Bearbeitet: ejk am 2 Jun. 2020
That still gives me ans = 4. I'm looking to get as many decimal places as possible.

Melden Sie sich an, um zu kommentieren.

madhan ravi
madhan ravi am 2 Jun. 2020

0 Stimmen

sprintf("%.55f",a/b)

5 Kommentare

ejk
ejk am 2 Jun. 2020
Unfortunately, that just gives me "4.0000000000000000000000000000000000000000000000000000000".
For some reason, if I do the division using the variables a and b (ie. a/b), the answer is always an integer, but if I copy and paste the numbers from the workspace into the command window and then divide, I get a floating point, which is odd.
It almost looks like MATLAB is treating the variable value and the raw number differently, although they are both of type double.
>> sprintf("%.55f",a/b)
ans =
"3.9999999999999986677323704498121514916419982910156250000"
ejk
ejk am 2 Jun. 2020
Right, but you probably just copied the values of a and b above right?
The problem is that for me, a and b are elements in a matrix, so when I set a = matrix(1,50) and b = matrix(1,51), then do a/b I get an integer.
Not sure why MATLAB handles matrix element values differently.
madhan ravi
madhan ravi am 2 Jun. 2020
upload the variables a and b as .mat file.
ejk
ejk am 2 Jun. 2020
Good idea. I've attached them here.

Melden Sie sich an, um zu kommentieren.

Gefragt:

ejk
am 2 Jun. 2020

Kommentiert:

ejk
am 2 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by