No safety in numbers

Konrad Hinsen hinsen@cnrs-orleans.fr
Thu, 21 Aug 2003 22:30:33 +0200


I am trying to write a larger piece of code using only type constraints f=
or=20
all the numbers, not specific types, in order to be able to choose the=20
precision as late as possible. This works rather well (something I can't =
say=20
of many other languages), but one problem I keep running into is constant=
s.

Something I need frequently is, for example, the Boltzman constant,=20
0.0083144708636327096 in my unit system. I can certainly type=20
0.0083144708636327096 everywhere in the code and make things work, litera=
ls=20
have the nice property of being overloaded. But for the sake of readibili=
ty,=20
I prefer to give this beast a name and have the explicit value only once =
in=20
my code. So I create a module "Constants" with something like

k_B =3D 0.0083144708636327096

The trouble is that k_B then becomes "Double" by default (or any other ty=
pe I=20
declare it to be). And this ruins all the code where I make a reference t=
o=20
it, with Hugs telling me "Inferred type is not general enough" because al=
l my=20
"generic" types become unified with Double.

The only solution I can see is to write a function "fromDouble" in analog=
y to=20
"fromInteger" and put this in front of all named constants. But how could=
 I=20
implement such a function?

I might also declare all my constants to be "Rational" and use "fromRatio=
nal",=20
but I don't know much about the "Rational" type. Do I have to worry about=
=20
insufficient or compiler-dependent precision?

Konrad.