Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

tcl code for decimal to thermometer conversion

202 views
Skip to first unread message

debolina ghosh

unread,
Mar 12, 2023, 2:54:38 PM3/12/23
to
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

Christian Gollwitzer

unread,
Mar 12, 2023, 3:44:32 PM3/12/23
to
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

Luc

unread,
Mar 12, 2023, 4:06:00 PM3/12/23
to
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
>>

debolina ghosh

unread,
Mar 12, 2023, 4:21:34 PM3/12/23
to
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

mango

unread,
Mar 12, 2023, 4:29:45 PM3/12/23
to
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}]

Rich

unread,
Mar 12, 2023, 10:55:09 PM3/12/23
to
Luc <l...@sep.invalid> 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.
Message has been deleted
Message has been deleted

Alex P

unread,
Mar 13, 2023, 6:26:43 AM3/13/23
to
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

dave bruchie

unread,
Mar 13, 2023, 9:44:23 AM3/13/23
to
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

Paul Walton

unread,
Mar 14, 2023, 1:26:06 PM3/14/23
to
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 ""]] ""]
Message has been deleted

Alex P

unread,
Mar 15, 2023, 5:55:11 AM3/15/23
to
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"
}

saitology9

unread,
Mar 15, 2023, 6:02:36 PM3/15/23
to
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



Alex P

unread,
Mar 16, 2023, 1:06:37 AM3/16/23
to
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)"
0 new messages