1
li.s $f1, 0.2
li.s $f2, 2.0
mul.s $f3, $f1, $f2

mov.s $f12, $f3 
li $v0, 2             # system call 2 to print float
syscall 

The code above gives me 0.40000001 instead of exactly 0.40000000. Is there a way to bypass this?

Sorry if the problem is too obvious as I'm very new to MIPS and assembly language.

Dong Cao-Huu
  • 53
  • 1
  • 8
  • 4
    `0.4` is not exactly representable in binary. – Jester Mar 19 '20 at 00:05
  • 2
    @Jester: Importantly, `0.2` isn't exactly representable either; while multiplication by 2.0 is lossless in binary floating point, it's doubling the underlying error in the representation of `0.2`. – ShadowRanger Mar 19 '20 at 00:42
  • @ShadowRanger: doubling the absolute error, leaving the relative error unchanged of course because it's exact multiplication. But yeah, the ultimate source of the error is that `1/5` doesn't have a power of 2 denominator, not from the multiply step. – Peter Cordes Mar 19 '20 at 02:58
  • @Jester: A plea for precise language: Using the code style usually indicates code, so that `0.4` is not mathematical 0.4 but the thing `0.4` means in code, which is a number translated to the machine/language format, which is 0.40000000000000002220446049250313080847263336181640625 in IEEE-754 binary64, 0.4000000059604644775390625 in binary32, so it is representable in binary floating-point. 0.4 is not representable in binary floating-point. `0.4` is. – Eric Postpischil Mar 21 '20 at 11:26

0 Answers0