Implementing floating point subtraction

后端 未结 2 1816
天涯浪人
天涯浪人 2021-01-19 06:26

all I am trying to implement a floating point arithmetic library and I have trouble understanding the algorithm of subtracting floats. I have implemented addition succesfull

相关标签:
2条回答
  • 2021-01-19 07:03

    If your addition code is correct, and your subtraction isn't, the problem is presumably in the two's complement and addition.

    Is it necessary to do the two's complement and addition, rather than subtraction?

    If that's not the problem, I'm having trouble with your algorithm. It's been a while since I did anything like this. Could you provide some details? More specifically, what is the hidden bit?

    It seems possible to me that the handling of the hidden bit is proper for addition but not subtraction. Could it be that you should set it in the f1 mantissa rather than the f2? Or negate the f1 mantissa instead of the f2?

    Without knowing what you're getting versus what you're expecting, and more details of the algorithm you're using, that's the best I can do.

    Edit: OK, I looked at the references in your comment. One thing you are failing to do in the supplied code is normalization. When adding, either the hidden bits overflow (shift mantissa to the left, increment exponent), or they don't. When subtracting, arbitrary parts of the mantissa can be zero. In decimal, consider adding 0.5E1 and 0.50001E1; you'd get 1.00001E1 and if you were to normalize you'd get 0.10001E2. When subtracting the 0.5E1 from 0.50001E1, you get 0.00001E1. Then you need to shift the mantissa to the left and decrement the exponent by as much as it takes, to get 0.1E-4.

    0 讨论(0)
  • 2021-01-19 07:04

    a-b == a+(-b), and unary minus is trivial, so I wouldn't even bother with binary minus.

    0 讨论(0)
提交回复
热议问题