No safety in numbers
Thu, 21 Aug 2003 22:30:33 +0200
I am trying to write a larger piece of code using only type constraints f=
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 =
of many other languages), but one problem I keep running into is constant=
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=
have the nice property of being overloaded. But for the sake of readibili=
I prefer to give this beast a name and have the explicit value only once =
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=
declare it to be). And this ruins all the code where I make a reference t=
it, with Hugs telling me "Inferred type is not general enough" because al=
"generic" types become unified with Double.
The only solution I can see is to write a function "fromDouble" in analog=
"fromInteger" and put this in front of all named constants. But how could=
implement such a function?
I might also declare all my constants to be "Rational" and use "fromRatio=
but I don't know much about the "Rational" type. Do I have to worry about=
insufficient or compiler-dependent precision?