• Decimal Q numbers : fractional numbers represented by entier + numberof fractional digits

    From Harald Oehlmann@wortkarg3@yahoo.com to comp.lang.tcl on Fri Mar 1 12:48:02 2024
    From Newsgroup: comp.lang.tcl

    Dear TCL math enthusiasts,

    I have programs to weight a minimum quantity using a scale.
    Up to now, I used double math:

    The scale has a certain resolution and returns a string like
    "8.24 g". It always has 2 fractional digits ("24" in this example).

    The minimum quantity is calculated from many factors and displayed to
    the users.

    What I did so far:
    set weightIs "8.24 g"
    set plan 6.8;set efficiency 1.2; set remainder 0.081
    set weightShould [expr {$plan * $efficiency - $remainder}]
    # the upper formular is arbitrary. It is just a calculation of multiple factors
    set weightIs [scan $weightIs "%f g"]
    set weightShouldDisplay [format %.2f $weightShould]

    if {$weightShould <= $weightIs} {#Ok}

    The last comparision is the issue. If the user sees:
    Minimum: 8.24
    it really starts at something like: 8.235
    So a remainder of 0.081 gives ok, but 0.078 gives not ok while
    displaying the same number.

    I also have an issue in documentation. The systems are validated and
    should be consistent.

    I also fear, that two doubles may have the same text representation. I
    am not sure, if this may happen. Kevin Kenny has worked a lot on the roundtrip, e.g. double->text->double gives the same result.

    But is it true, that two doubles created from strings with same number
    of digits are equal, if the strings are equal ?
    set t1 [format %02f $d1]
    set t2 [format %02f $d2]
    set dt1 [scan $t1 %f]
    set dt2 [scan $t2 %f]
    if {$dt1 == $dt2} then $t1 eq $t2


    So, I thought to use exact decimal quotational math. Each number is represented by an entier and a fractional count

    8.24 -> 824 2

    (This is the q notation I know from embedded systems, but in decimal not binary)

    Limited math like multiplication, addition and comparison are possible.

    Is this a good idea?
    Is there already a library for this type of math?
    I suppose, a custom TCL type may be defined, that would be weired ;-).

    I know, something like this is used in accounting.
    The rounding of operations is well defined in this case.
    Banks are forced to take 3 fractional decimal digits for any operation.

    Any idea welcome !

    Thank you and take care,
    Harald
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Rich@rich@example.invalid to comp.lang.tcl on Fri Mar 1 22:42:58 2024
    From Newsgroup: comp.lang.tcl

    Harald Oehlmann <wortkarg3@yahoo.com> wrote:
    So, I thought to use exact decimal quotational math. Each number is represented by an entier and a fractional count

    8.24 -> 824 2

    Is there already a library for this type of math?

    Yes, Tcllib math::decimal https://core.tcl-lang.org/tcllib/doc/tcllib-1-18/embedded/www/tcllib/files/modules/math/decimal.html

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Harald Oehlmann@wortkarg3@yahoo.com to comp.lang.tcl on Sun Mar 3 18:54:45 2024
    From Newsgroup: comp.lang.tcl

    Am 01.03.2024 um 23:42 schrieb Rich:
    Harald Oehlmann <wortkarg3@yahoo.com> wrote:
    So, I thought to use exact decimal quotational math. Each number is
    represented by an entier and a fractional count

    8.24 -> 824 2

    Is there already a library for this type of math?

    Yes, Tcllib math::decimal https://core.tcl-lang.org/tcllib/doc/tcllib-1-18/embedded/www/tcllib/files/modules/math/decimal.html


    Thanks, Rich, that was exactly what I was looking for!
    Great that you know all those details over many years!

    Take care,
    Harald
    --- Synchronet 3.20a-Linux NewsLink 1.114