Tcl Library Source Code

Check-in [23597e6af2]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fixed a math error. Added regression test for non-absolute unit changes
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | odie
Files: files | file ages | folders
SHA1: 23597e6af238266b80a4d4c7ab5907969153c309
User & Date: tne 2016-11-05 19:37:36.792
Context
2016-11-06
12:28
Eliminated a duplication of effort between a list and an array by utilizing a dict check-in: cbbadebbdc user: tne tags: odie
2016-11-05
19:37
Fixed a math error. Added regression test for non-absolute unit changes check-in: 23597e6af2 user: tne tags: odie
15:48
Public procs in the units module now use Tcl's conventional argument processor Added tests for the new dimensional analysis features check-in: 757744956d user: tne tags: odie
Changes
Unified Diff Ignore Whitespace Patch
Changes to modules/units/units.tcl.
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    #  to primitive units
    set reducedValue [::units::reduce $value]
    set reducedTarget [::units::reduce $targetUnits]
    
    set operation {}
    if {[llength $reducedValue]==4 && [lindex $reducedValue 1] in {+ -}} {
	if {[lindex $reducedValue 1] eq "+"} {
	    lappend operation [lindex $reducedValue 0] - [lindex $reducedValue 2]
	} else {
	    lappend operation [lindex $reducedValue 0] + [lindex $reducedValue 2]
	}
	set reducedValue [reduce [lindex $reducedValue 3]]
	lappend operation * [lindex $reducedValue 0] /
    } else {
	lappend operation [lindex $reducedValue 0] /
    }
    if {[llength $reducedTarget]==4 && [lindex $reducedTarget 1] in {+ -}} {







|

|







90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    #  to primitive units
    set reducedValue [::units::reduce $value]
    set reducedTarget [::units::reduce $targetUnits]
    
    set operation {}
    if {[llength $reducedValue]==4 && [lindex $reducedValue 1] in {+ -}} {
	if {[lindex $reducedValue 1] eq "+"} {
	    lappend operation ( [lindex $reducedValue 0] - [lindex $reducedValue 2] )
	} else {
	    lappend operation ( [lindex $reducedValue 0] + [lindex $reducedValue 2] )
	}
	set reducedValue [reduce [lindex $reducedValue 3]]
	lappend operation * [lindex $reducedValue 0] /
    } else {
	lappend operation [lindex $reducedValue 0] /
    }
    if {[llength $reducedTarget]==4 && [lindex $reducedTarget 1] in {+ -}} {
Changes to modules/units/units.test.
521
522
523
524
525
526
527




528
529
530
531











































532
533
534
::units::new farhenheit {- 459.67 rankine}
::units::new celsius {- 273.15 kelvin}

test units-11.0 {Test kelvin -> rankine} {
    format %5.3f [::units::convert {451 rankine} kelvin]
} {250.556}





test units-11.1 {Test kelvin -> rankine} {
    format %5.3f [::units::convert {18 celsius} farhenheit]
} {50.000}












































#-----------------------------------------------------------

testsuiteCleanup







>
>
>
>
|
|


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>



521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
::units::new farhenheit {- 459.67 rankine}
::units::new celsius {- 273.15 kelvin}

test units-11.0 {Test kelvin -> rankine} {
    format %5.3f [::units::convert {451 rankine} kelvin]
} {250.556}

test units-11.2 {Test farhenheit -> farhenheit} {
    format %5.3f [::units::convert {50 farhenheit} farhenheit]
} {50.000}

test units-11.3 {Test kelvin -> rankine} {
    format %5.3f [::units::convert {50 celsius} celsius]
} {50.000}


proc C_to_K temp {
  return [format %5.3f [expr {$temp-273.15}]]
}
proc F_to_C temp {
  return [format %5.3f [expr {($temp-32)*5.0/9.0}]]
}
proc C_to_F temp {
  return [format %5.3f [expr {$temp*9.0/5.0+32}]]
}

###
# Cherry pick some known values
###
foreach {value_a unit_a value_b unit_b} {
  0 celsius 32 farhenheit
  100 celsius 212 farhenheit
} {
  set test_a []
  set test_b []
  test units-11.4.$value_a [list Test Celcius -> Fahrenheight $value_a] {
    expr {int(round([::units::convert [list $value_a $unit_a] $unit_b]))}
  } $value_b
  test units-11.4.$value_b [list Test Fahrenheight -> Celcius $value_b] {
    expr {round([::units::convert [list $value_b $unit_b] $unit_a])}
  } $value_a

}
puts [list TESTING 0 C]
puts [list [C_to_F 0] [::units::convert [list 0 celsius] farhenheit]]
puts [list TESTING 100 C]

puts [list [C_to_F 100] [::units::convert [list 100 celsius] farhenheit]]

for {set test_temp -50} {$test_temp < 150} {incr test_temp 10} {
  test units-11.5.$test_temp [list Test Fahrenheight -> Celcius $test_temp] {
    format %5.3f [::units::convert [list $test_temp farhenheit] celsius]
  } [F_to_C $test_temp]
  test units-11.5.$test_temp [list Test Celcius -> Fahrenheight $test_temp] {
    format %5.3f [::units::convert [list $test_temp celsius] farhenheit]
  } [C_to_F $test_temp]
}

#-----------------------------------------------------------

testsuiteCleanup