For this task, I am using a patched version of julia which redefines
the floating point literals. The conversion of all literals is done at
the parser level and one can switch the default floating point
precision on the fly.
The base julia library has never been designed to handle this type
conversion but the fix is relatively non-invasive and very few changes
were needed to make the system work (e.g., base/irrationals.jl,
base/printf.jl). In my codes, I determine the default type by using
typeof(1.0) call, please take a look at adapgaus.jl and
legepols.jl. By rewriting rand, eye, etc. routines, one can make a
generic code that is fully reinterpreted in either BigFloat or Float32
as needed. Actually, I have been using this patch for tightening
quadratures tables that were build in Float64.
For julia 0.6.0 release, I put together my
changes into zg/promo branch which you can download here:
git clone http://github.com/zgimbutas/julia.git
cd julia
git checkout zg/promo
make
After the patched version of julia is installed (you might want to use
make CFLAGS=-Wno-error on Xcode 9 + MacOS High Sierra to handle a
libcurl compilation bug), please clone the user space tools from
git clone http://github.com/zgimbutas/default-promo.git
This package provides several scripts for switching between Float16,
Float32, Float64 and BigFloat literals: setenv_float16.jl,
setenv_float32.jl, setenv_float64.jl and setenv_bigfloat.jl.
For example:
include("setenv_bigfloat.jl")
println("\n===basic arithmetic==")
println("(1.0+1/3-2)*2=",(1.0+1/3-2)*2)
println("2/3=",2/3)
println("eps(1.0)=", eps(1.0))
println("\n===elementary functions==")
println("sin(1.0)=", sin(1.0))
println("sin(1)=", sin(1))
println("exp(1.0)=", exp(1.0))
println("exp(1)=", exp(1))
println("\n===constants and irrationals==")
println("4*atan(1.0)=", 4*atan(1.0))
println("pi=", pi)
println("2*pi=", 2*pi)
println("2.0*pi=", 2.0*pi)
println("pi-4*atan(1.0)=", pi-4*atan(1.0))
The output should look this:
===literals==
typeof(1.0)=BigFloat
typeof(1.0e0)=BigFloat
===basic arithmetic==
(1.0+1/3-2)*2=-1.333333333333333333333333333333333333333333333333333333333333333333333333333322
2/3=6.666666666666666666666666666666666666666666666666666666666666666666666666666695e-01
eps(1.0)=1.727233711018888925077270372560079914223200072887256277004740694033718360632485e-77
===elementary functions==
sin(1.0)=8.414709848078965066525023216302989996225630607983710656727517099919104043912398e-01
sin(1)=8.414709848078965066525023216302989996225630607983710656727517099919104043912398e-01
exp(1.0)=2.718281828459045235360287471352662497757247093699959574966967627724076630353555
exp(1)=2.718281828459045235360287471352662497757247093699959574966967627724076630353555
===constants and irrationals==
4*atan(1.0)=3.141592653589793238462643383279502884197169399375105820974944592307816406286198
pi=π = 3.1415926535897932384626433832795028841971693993751058209749...
2*pi=6.283185307179586476925286766559005768394338798750211641949889184615632812572396
2.0*pi=6.283185307179586476925286766559005768394338798750211641949889184615632812572396
pi-4*atan(1.0)=0.000000000000000000000000000000000000000000000000000000000000000000000000000000
or if include(“setenv_float32.jl”) is used
===literals==
typeof(1.0)=Float32
typeof(1.0e0)=Float32
===basic arithmetic==
(1.0+1/3-2)*2=-1.3333333
2/3=0.6666667
eps(1.0)=1.1920929e-7
===elementary functions==
sin(1.0)=0.84147096
sin(1)=0.84147096
exp(1.0)=2.7182817
exp(1)=2.7182817
===constants and irrationals==
4*atan(1.0)=3.1415927
pi=π = 3.14159...
2*pi=6.2831855
2.0*pi=6.2831855
pi-4*atan(1.0)=0.0
Some work is needed to debug the base library, the type conversion
will stress it in non-trivial way (for example, base/irrationals.jl
have build in Float64 conversions that I had to overload). For linear
algebra and FFTs, some generic functions are missing as well (svd,
eig, etc.). But I was able to do some non-trivial experiments this
way, and hope, it could be a good starting point.
More extensive tests are in test_float16.jl, test_float32.jl,
test_float64.jl, test_bigfloat.jl with corresponding results located
in *.output files.
Hope this helps.