开发者

Numeric instability

开发者 https://www.devze.com 2023-02-07 08:03 出处:网络
I\'m doing some Linear programming exercises for the course of Algorithms, and in doing this I\'m solving manually many operations with fractions. In doing this I realized that a human being don\'t su

I'm doing some Linear programming exercises for the course of Algorithms, and in doing this I'm solving manually many operations with fractions. In doing this I realized that a human being don't suffer from numeric instability: we just keep values in fractional representation, and we finally evaluate (possibly by using a calculator) the va开发者_开发问答lue of expressions.

Is there any technique that does this automatically?

Im thinking of something which achieves some kind of symbolic computation, simplifies the numbers internally and finally yields the value only during the evaluation of an expression.


Boost contains a rational number library here which might be of help.


In Python you can have a look at fractions:

import fractions
a = fractions.Fraction(2,3)

a*2
# Fraction(4, 3)

a**2
# Fraction(4, 9)

'Value: %.2f' % a
# 'Value: 0.67'
0

精彩评论

暂无评论...
验证码 换一张
取 消