• tcl code for decimal to thermometer conversion

    From debolina ghosh@21:1/5 to All on Sun Mar 12 11:54:35 2023
    hi Folks,

    I am a new tcl code user. I need to write a tcl code to convert decimal into thermometer code . Any guidance ?

    Regards,
    Deb

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Luc@21:1/5 to Christian Gollwitzer on Sun Mar 12 17:05:55 2023
    On Sun, 12 Mar 2023 20:44:27 +0100, Christian Gollwitzer wrote:

    Am 12.03.23 um 19:54 schrieb debolina ghosh:
    hi Folks,

    I am a new tcl code user. I need to write a tcl code to convert
    decimal into thermometer code . Any guidance ?

    It's totally unclear what you mean by "decimal" and "thermometer". Can
    you give a concrete example?

    Christian


    I bet the original poster meant from Celsius to Fahrenheit.

    First, you need the formula:

    https://www.calculatorsoup.com/calculators/conversions/celsius-to-fahrenheit.php

    °F = °C * 9/5 + 32

    In Tcl syntax,

    set Fahrenheit [expr [expr Celsius * [expr 9 / 5] + 32]

    Or simpler:

    set Fahrenheit [expr [expr $Celsius * 1.8] + 32]

    Testing:

    % set Celsius 32
    % set Fahrenheit [expr [expr $Celsius * 1.8] + 32]
    89.6

    % set Celsius 14
    % set Fahrenheit [expr [expr $Celsius * 1.8] + 32]
    57.2

    % set Celsius 78
    % set Fahrenheit [expr [expr $Celsius * 1.8] + 32]
    172.4

    If you want to convert from Fahrenheit to Celsius, the opposite formula
    is here:

    https://www.calculatorsoup.com/calculators/conversions/fahrenheit-to-celsius.php


    --
    Luc


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Christian Gollwitzer@21:1/5 to All on Sun Mar 12 20:44:27 2023
    Am 12.03.23 um 19:54 schrieb debolina ghosh:
    hi Folks,

    I am a new tcl code user. I need to write a tcl code to convert decimal into thermometer code . Any guidance ?


    It's totally unclear what you mean by "decimal" and "thermometer". Can
    you give a concrete example?

    Christian

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From mango@21:1/5 to debolina ghosh on Sun Mar 12 13:29:42 2023
    On Sunday, March 12, 2023 at 1:21:34 PM UTC-7, debolina ghosh wrote:
    On Sunday, 12 March 2023 at 12:44:32 UTC-7, Christian Gollwitzer wrote:
    Am 12.03.23 um 19:54 schrieb debolina ghosh:
    hi Folks,
    [snip]

    Decimal 0 -> Thermometer 0000
    Decimal 1 -> Thermometer 0001
    Decimal 2 -> Thermometer 0011
    Decimal 3 -> Thermometer 0111
    Decimal 4 -> Thermometer 1111

    How about:
    set thermometer [expr {(1 << $decimal) - 1}]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From debolina ghosh@21:1/5 to Christian Gollwitzer on Sun Mar 12 13:21:32 2023
    On Sunday, 12 March 2023 at 12:44:32 UTC-7, Christian Gollwitzer wrote:
    Am 12.03.23 um 19:54 schrieb debolina ghosh:
    hi Folks,

    I am a new tcl code user. I need to write a tcl code to convert decimal into thermometer code . Any guidance ?

    It's totally unclear what you mean by "decimal" and "thermometer". Can
    you give a concrete example?

    Christian
    hi Christian,

    I want to convert Decimal code to thermometer code like below :

    Decimal 0 -> Thermometer 0000
    Decimal 1 -> Thermometer 0001
    Decimal 2 -> Thermometer 0011
    Decimal 3 -> Thermometer 0111
    Decimal 4 -> Thermometer 1111

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Rich@21:1/5 to Luc on Mon Mar 13 02:55:03 2023
    Luc <[email protected]d> wrote:
    set Fahrenheit [expr [expr Celsius * [expr 9 / 5] + 32]

    Why three expr's?

    Note that "Celsius" will cause an error due to omitted $.

    And, assuming everything was fixed, the above will return an integer if
    Celsius contains an integer, which may not be what is intended for a temperature conversion. Either wrap the Celsius in a double() {double($Celsius)} to force it to floating point, or change one of 9 or
    5 to 9.0 or 5.0 so the rest of the statement promotes to floating
    point.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alex P@21:1/5 to All on Mon Mar 13 03:06:09 2023
    mango meant:

    set decimal 7
    set octal [expr {(1 << $decimal) - 1}]
    puts thermometer\ [format %020b $octal]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alex P@21:1/5 to All on Mon Mar 13 03:13:57 2023
    mango meant

    set decimal 7
    set thermal [format %020b [expr {(1 << $decimal) - 1}]]
    puts thermometer\ $thermal

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alex P@21:1/5 to All on Mon Mar 13 03:26:40 2023
    following mango's post:

    set decimal 7

    set thermal [format %020b [expr {(1 << $decimal) - 1}]]
    puts thermometer\ $thermal

    set inverted [string replace $thermal 0 end-$decimal][string range $thermal 0 end-$decimal]
    puts thermometer\ $inverted

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dave bruchie@21:1/5 to All on Mon Mar 13 06:44:21 2023
    If you want a string of characters try the "repeat" option of the "string" command. It lets you use any character, not just "1". Perhaps *** or ---- or _____ might look better.

    If you are learning TCL, consider using the "for" or "while" commands to build your own counted loop to build a character string.

    Dave B

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Paul Walton@21:1/5 to Alex P on Tue Mar 14 10:26:00 2023
    On Monday, March 13, 2023 at 6:26:43 AM UTC-4, Alex P wrote:
    set inverted [string replace $thermal 0 end-$decimal][string range $thermal 0 end-$decimal]
    puts thermometer\ $inverted

    Another method:
    set inverted [join [lreverse [split $thermal ""]] ""]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alex P@21:1/5 to All on Wed Mar 15 02:55:06 2023
    Yes, your method is universal for inverting strings. Mine is mostly for this use case.
    We might compose some receipts for Tcl cookbook:


    proc thermal {dec {len 20}} {
    return [format %0${len}b [expr {(1 << $dec) - 1}]]
    }
    #_______________________

    proc invertSubst {str idx} {
    return [string replace $str 0 end-$idx][string range $str 0 end-$idx]
    }
    #_______________________

    proc invertStr {str} {
    return [join [lreverse [split $str {}]] {}]
    }
    #_______________________

    # testing

    set ckl 100000
    foreach d {0 1 8 10 19 20 30} {
    set t [thermal $d]
    set time1 [time {set t1 [invertSubst $t $d]} $ckl]
    set time2 [time {set t2 [invertStr $t]} $ckl]
    puts "decimal: $d"
    puts "thermal: $t"
    puts " by Sub: $t1 ($time1)"
    puts " by Str: $t2 ($time2)\n"
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alex P@21:1/5 to All on Wed Mar 15 02:45:14 2023
    Yes, your method is universal for inverting strings. Mine is mostly for this use case.
    We might compose some receipts for Tcl cookbook:

    proc thermal {dec {len 20}} {
    return [format %0${len}b [expr {(1 << $dec) - 1}]]
    }
    #_______________________

    proc invertSubst {str idx} {
    return [string replace $str 0 $idx][string range $str 0 $idx]
    }
    #_______________________

    proc invertStr {str} {
    return [join [lreverse [split $str {}]] {}]
    }
    #_______________________

    # testing

    set ckl 100000
    foreach d {0 1 8 10 19 20 30} {
    set t [thermal $d]
    set time1 [time {set t1 [invertSubst $t end-$d]} $ckl]
    set time2 [time {set t2 [invertStr $t]} $ckl]
    puts "decimal : $d"
    puts "thermal : $t"
    puts "with Subst: $t1 ($time1)"
    puts "with Str : $t2 ($time2)\n"
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From saitology9@21:1/5 to Alex P on Wed Mar 15 18:02:30 2023
    On 3/15/2023 5:55 AM, Alex P wrote:
    Yes, your method is universal for inverting strings. Mine is mostly for this use case.
    We might compose some receipts for Tcl cookbook:




    Here is my take on it. It is faster too:

    % proc to_thermal {num {len 10} {filler 0}} {
    return "[string repeat $filler [expr {$len - $num}]][string repeat 1
    $num]"
    }


    % time {to_thermal 30} 100000
    0.49901 microseconds per iteration

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alex P@21:1/5 to All on Wed Mar 15 22:06:34 2023
    So that we update the Tcl cookbook :)

    proc thermal {dec {len 20}} {
    return [format %0${len}b [expr {(1 << $dec) - 1}]]
    }
    #_______________________

    proc invertSubst {str idx} {
    return [string replace $str 0 end-$idx][string range $str 0 end-$idx]
    }
    #_______________________

    proc invertStr {str} {
    return [join [lreverse [split $str {}]] {}]
    }
    #_______________________

    proc to_thermal {num {len 10} {filler 0}} {
    return "[string repeat $filler [expr {$len - $num}]][string repeat 1 $num]"
    }
    #_______________________

    # testing

    set ckl 100000
    foreach d {0 1 8 10 19 20 30} {
    set time0 [time {set t [thermal $d]} $ckl]
    set timet [time {set t2 [to_thermal $d]} $ckl]
    set time1 [time {set t1 [invertSubst $t $d]} $ckl]
    set time2 [time {set t2 [invertStr $t]} $ckl]
    puts "decimal: $d"
    puts "therm 1: $t ($time0)"
    puts "therm 2: $t2 ($timet)"
    puts " by Sub: $t1 ($time1)"
    puts " by Str: $t2 ($time2)\n"
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)