I’m announcing SuffixConversion.jl, which aims to make it easier to write generic code involving numeric literals without unintentional type promotion. An example:
using SuffixConversion
function addhalf(x)
@suffix FT = typeof(x)
return x + 0.5_FT
end
Note that this still seems to lose precision in many cases if the starting number is not exactly represented as a Float64 literal, e.g.:
julia> using DecFP
julia> @suffix Dec128
SuffixConversion.SuffixConverter{Dec128}()
julia> 0.1_Dec128
0.1000000000000000055511151231257827
(You work around this for BigFloat only by converting to string first. You could do this for all types, of course, but then performance would really suffer.)
FT(0.5). If you have a rational number that is not exactly representable by a Float64 literal, however, you’re better off using a rational expression, e.g. FT(1//10) rather than FT(0.1). (See my post above.)