Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2005-03-29 05:35:28

"Andras Erdei" <aerdei_at_[hidden]> wrote

> after i failed to convince people that the current boost::rational is useless,

As has been previously stated. it is not the rational Concept that is useless
but rather the limitations imposed by finite number representations of the
I feel that boost::rational is being unfairly blamed for that.

One could create various integer types that address the problem:
An integer that is bounded , but in ops the result_type can hold any computed
result accurately:

int16 operator *(int8,int8) ;
int16 operator +(int8,int8) ;
int16 operator -(int8,int8) ;
rational<int8> operator / (int8,int8) ; // ... :-)
int8 quotient (int8,int8) ; mimicks inbuilt int operator / behaviour

int32 operator+(int16,int16) ;// etc
int64 operator* (int32,int32) // etc

 and using boost::numeric_converter to downcast, makes this a basis for a family
of integers.

> - did a boost::rational implementation (moved to
> which does +, -, * and / _at the same speed_ as int +, -, * and / -- if
> rational computation (without hardware support) has the same speed as
> integer, then why was floating point (which is much worse than rational)
> ever introduced in the first place? isn't this suspicious?

IMO rational is useful for storing Exactly a limited range of numbers, float for
storing a larger range approximately. Therefore I disagree with a rounding
policy on rationals. The roles are different. Rationals can be compared exactly
whereas this is not very useful with floats, which is a major advantage for
It is possible to cast from rational to float, it is unwise to make a cast from
float to rational, because the rational gives a precision guarantee that the
float doesnt.

> - did a test with boost::rational, executing +, -, * and / with 10000 random
> numbers; + and - gave the correct result only in 44 cases, and * and / in 79
> cases -- does it sound useable?

Except that rational numbers used in practise are not entirely random. I would
guess that 1/2 is most common, 1/3 etc.

> sorry, but feeling frustrated :O)

FWIW your protestations have pointed out potential errors in some of my code,
which is a good thing:-)

Andy Little

Boost list run by bdawes at, gregod at, cpdaniel at, john at