1

As the title says, I'm trying to print a float value in assembly mips. I'm not facing any errors, it's just that the value that ends up showing on the console is not the same one that I put when writing the program. Here's my code:

.data
    PI: .float 3.14
.text
main:
    li $v0, 2
    lwc1 $f12, PI 
    syscall
    li $v0, 10
    syscall

The console shows 3.14000010 after executing this. Is there anyway I can fix this, or is there something I'm missing? Thank you.

SimpleBeat
  • 747
  • 1
  • 10
  • 20
Argy
  • 11
  • 1
  • 3
  • 2
    Not all numbers can be represented exactly as a floating point value. – Michael Petch Apr 25 '17 at 15:18
  • 2
    Possible duplicate of [Is floating point math broken?](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Ped7g Apr 25 '17 at 15:23
  • As "3.14" is enough accurate for you, I don't see any problem with another 1e-7 added.. actually it makes your constant closer to real PI value. There's no way to limit HW float/double values to certain amount of decimals (with `printf`-like functions you can limit the output, but not the real value). You have to use different value format for that (string, or fixed point math, or arbitrary decimal format), this is crucial for example for monetary applications, to have exact decimals. You may need better than 32/64 bit precision sometimes in physics and similar. – Ped7g Apr 25 '17 at 15:28
  • A 32-bit `float` can store only about 2^32 of the infinitely many real numbers within the representable range of `float`. The floating point scheme is a trade-off between range and accuracy. – Weather Vane Apr 25 '17 at 17:54
  • Thank you for the heads up everybody. – Argy Apr 25 '17 at 19:52

0 Answers0