floating point accuracy in MIPS assembly

Luqman

I wrote two code files in MIPS assembly for the expression below:

R(n) = (i to n) SUM { (i+2)/(i+1- 1/i) - i/(i+ 1/i) }

One code calculates the whole expression R(n) as summation and gives the result.

The second code first calculates first term, i.e., (i+2)/(i+1- 1/i) in a loop and then calculates the second term, i.e., i/(i+ 1/i) in another loop. It then simply subtracts the two summations.

Following are the results for the two programs for different values of n:

Program 1:

 N        Result
-----------
10        5.07170725

100       7.41927338

1000      9.72636795

10000    12.02908134

100000   14.33149338

1000000  16.63462067

Program 2:

 N       Result
---------
10       5.07170773

100      7.41923523

1000     9.72259521

10000   12.31250000

100000   8.61718750

1000000  6.50000000

Program 1 is giving more accurate results (compared with Wolfram Alpha results for R(n)). Why does Program 2 gives odd results here for large values of n? My question is related to floating point precision here.

Note: I am using single-precision numbers.

aka.nice

Say you have un=an-bn and you want sum(un)

lim an -> 1 when n -> infinity so the sum of P terms tends to P+cte_a, same for bn, the sum tends to P+cte_b

When you differentiate the two, (P+cte_a) - (P+cte_b), you should mathematically retrieve sum(un).

But with floating point, that's not what happens, because (P+cte_a) is rounded to nearest float. And the bigger P is, the less float(P+cte_a)-float(P) will be close to cte_a...

To convince yourself, try to evaluate these ops:

10.0f+0.1f-10.0f
100.0f+0.1f-100.0f
...
1.0e7f+0.1f-1.0e7

lim un -> 1/n when n -> infinity, so program 1 does a bit better...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

MIPS Assembly (MARS 4.5): floating point arithmetric answer is weird

Assembly MIPS: Cannot debug floating-point power function

Floating point accuracy

Find floating point accuracy

Managing floating point accuracy

Floating point accuracy with different languages

Elixir floating point division accuracy

Assembly floating point math

Floating point assembly on intel processor

assembly floating point code error

Groovy vs Java - difference in floating point accuracy

how set numpy floating point accuracy?

Difference between double and float in floating point accuracy

Compare a Floating Point value to 1 in MIPS assembler

Integer Implementation of Floating-Point Addition In MIPS

How to print floating point numbers from assembly?

Calling printf() in assembly causes a 'floating point exception'

comparing accuracy for calculating floating point with x*0.1 and x/10.0

Computing floating point accuracy (K&R 2-1)

How to increase accuracy of floating point second derivative calculation?

How to change the accuracy of a floating point number in a cell in QTableWidget?

Best possible accuracy for single precision floating point division

Strange issue with floating point accuracy on ARM64

Accuracy of floating point calculations: is something converting rational -> double-float?

'Find' function working incorrectly, have tried floating point accuracy resolution

Error in MIPS division with single precision floating point numbers

Preserving Accuracy When Formatting a Floating Point Number to Display With Two-point Precison (Java)

Calling an assembly language function causes "floating point stack check" exception

Harsh differences in generated assembly of floating-point comparisons < and >=