ANN: ChangePrecision.jl



ChangePrecision.jl, which has been mentioned recently on a couple of other threads, is now a registered package.

This package allows you to effectively change the “default” precision in a large body of Julia code, simply by doing e.g. @changeprecision Float32 include("foo.jl") or, for example:

using ChangePrecision
@changeprecision BigFloat begin
    x = pi/2
    y = 2.1 * x * rand(3,3) \ linspace(0,1,3)

and all floating-point literals, as well as many functions like rand() or expressions like 3/2 will now default to the new type (here BigFloat) rather than Float64. Code that uses explicit types, like rand(Float64), will not be affected.

This is mainly meant for informal user “scripts” rather than library code, since library code should typically be written in a type-generic manner (inferring the desired precision from function arguments rather than hardcoding it).

Change default precision

For functions, should the definitions, or call point, or both be wrapped in the macro?


The definitions.

As it says in the README, it doesn’t “look inside” functions that you call and rewrite them (there is no way for a macro to do this). The @changeprecision macro only transforms expressions that explicitly appear in the code you apply it to (or code inserted by include).

(The function calls that it transforms are only a specific set of Base functions that the macro knows about…and it’s not actually changing those functions, just calling a different method of them.)


Not to dismiss your work, but I am skeptical of the utility of packaging this (other than for demonstrating how powerful Julia is so that it allows you to do this). Having a registered package may just divert some inexperienced users from the right solution (writing generic code, yes, even for scripts).


So you suggest to write e.g. T(1.32) everywhere in a quick and dirty script with lots of numeric constants? And add tests that make sure you did not forget a T somewhere?

(In my scripts I just write 1.32 and only add the T when I need it. ChangePrecision seems handy.)

OTOH sometimes I really want Rational{BigInt}(132, 100) instead of 1.32 and thats seems impossible with a macro.


It is hard to say without the code, but if you have lots of numeric constants, you might want to use some container structure to organize them. See, for example, the implementation of Base.Math.JuliaLibm.log1p.