REAL type support #145
Labels
area/types
Related to rasn’s types for ASN.1
help wanted
Extra attention is needed
kind/enhancement
New feature or request
Continuing from the issue #58 and writing some ideas
I also noticed that real type is missing, but it might not be actually that hard to add (while some codecs might be)!
In ASN.1, real type is often approximated and not precise. Usually a single or double-precision approximation has been used to limit the size in codecs. The X.680 definition itself does not set any limits, but for practical reasons codecs often do.
Usually the real is presented with integers. Instead of one integer, you have three integers.
Representing real type is like bit shifting, but instead moving the comma. The shifting changes the exponent relatively moving the comma, so in the end you have just integers.
Based on that, maybe the easiest approach would be to provide Real as Enum type with following options:
Internally
rasn
could contain thef64
conversion to ASN.1 Real value as three integers, possibly with sign. BER seems to use sign with unsigned integers when binary encoding.Mapping from
f64
type might be good enough for default, since it is sufficient for double-precision approximation.Any application which requires higher precision is more likely to use rations with integers anyway in current Rust.
To support very large numbers, BigInt could be used to present these three integers internally.
For larger values than
f64
, we could provide alternatively something likeReal_number::from_ints
function which takes three integers to construct the ASN.1 Real. BigInt as mantissa, u8 as base (2 or 10) and BigInt as exponent will already give us practically as much range as hardware is able to handle.Alternatively, BigUint as mantissa and additional sign parameter. For this we might need to study which (sign as mark with unsigned integer or signed integer) is more used in the codecs.
It is up to the user of
rasn
to calculate these three integers if they need higher accuracy thanf64
.Maybe this REAL type overall would be good to add at the same time when the possibly for optional BigInt usage is added and end user could configure to use primitive integers instead, if we want to consider the performance of REAL values as well.
It might also take time before we would have other than
f32
orf64
types.There is a recent RFC coming with additional float types: rust-lang/rfcs#3451
The text was updated successfully, but these errors were encountered: