Skip to content

Significant digit control #37

@alexforencich

Description

@alexforencich

Seems like fastnum has rescale and quantize operations for moving the decimal point around, with rescale taking an integer and quantize using the exponent of another fastnum type. But, these are both absolute operations relative to the decimal point. It would be useful I think to have some additional operations along similar lines.

Relative shift of the decimal point: basically a fast multiply/divide by 10, like Python's decimal scaleb(). I think this would basically just adjust the exponent without touching the coefficient.

Relative digit shift: more or less the same thing, but applied to the coefficient, like Python's decimal shift().

Integer log10: verilog has $clog2(), which computes ceil(log2(x)) and tells you how many address lines you need for a given number of elements ($clog2(8) == $clog2(7) == 3). Might be useful to have a clog10 that does something similar here, and would be much faster to compute than a true log10. I think this would be ceil(log10(coefficient)) + exponent.

Get number of significant digits: in a similar vein, provide a function which will return the number of significant digits in the coefficient, which will be independent of the exponent. I think this would just be ceil(log10(coefficient)). I guess this is already implemented as digits_count.

Set number of significant digits: provide a function which will rescale the coefficient to provide the specified number of significant digits, fixing the exponent accordingly so the value is the same (aside from potential truncation).

I think most of this would be relatively simple wrappers on top of existing functionality. It's also possible I've missed some stuff in the documentation, and most of this is already doable.

Incidentally, I think you can rework your ln/log10/log2 implementations to split apart the exponent and coefficient and handle those parts separately, which should significantly reduce the number of reduce operations. Basically ln(x*10^y) = ln(x) + y*ln(10). Unless of course I am missing something about the implementation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    proposalA proposal and request for comments.questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions