milankl / SoftPosit.jl

A posit arithmetic emulator.
MIT License
47 stars 9 forks source link

exp, log, sin, cos, tan support #37

Closed milankl closed 5 years ago

milankl commented 5 years ago

Although the SoftPosit C-library does not have trigonometric functions implemented yet, we should consider a back and forth conversion as an intermediate work-around

sin(x::Posit16) = Posit16(sin(Float64(x)))

etc. Converting to Float32 should in theory work too as the dynamic range is slightly wider than for Posit32.

giordano commented 5 years ago

That's cheating :stuck_out_tongue:

milankl commented 5 years ago

I did it anyway. It turns out that correct rounding is actually super difficult with sin, cos etc.

julia> sin(π)
1.2246467991473532e-16

julia> Posit8(sin(π))
Posit8(0x01)

julia> Posit32(sin(π))
Posit32(0x0001c699)

julia> sin(Posit8(1π))
Posit8(0x01)

julia> sin(Posit32(1π))
Posit32(0x00710b46)

which is not the fault of Posits (they actually want to to implement error-free rounding in the posit standard) but due to the underlying errors with floats ... Well, one has to keep that in mind.

milankl commented 5 years ago

I kept it simple and only defined exp,exp2,exp10,log2,log10,sin,cos,tan - anything else?

giordano commented 5 years ago

I think they should be fine to start with

giordano commented 5 years ago

Oh, there are expm1 and log1p that are often used, too