summaryrefslogtreecommitdiff
path: root/lib/lib/Math.tcl
blob: c59f2a863dc6a9610c00db51b631c06df69ad81c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
#!/usr/bin/tclsh

############################################################################
#    Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012 by Martin OŇ°mera     #
#    martin.osmera@gmail.com                                               #
#                                                                          #
#    Copyright (C) 2014 by Moravia Microsystems, s.r.o.                    #
#    martin.osmera@moravia-microsystems.com                                #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################


# >>> File inclusion guard
if { ! [ info exists _MATH_TCL ] } {
set _MATH_TCL _
# <<< File inclusion guard
# --------------------------------------------------------------------------
# DESCRIPTION
# Primarily implements convertions between numeric systems and angle units.
# --------------------------------------------------------------------------

## ----------------------------------------------------------------------
## Converts between numeric systems and checks numbers types
 #
 # Supported num. systems: binary, octal, decimal, hexadecimal and ASCII.
 # note: Excepting H->Q and Q->H, are all convertions computed
 #       directly (for speed improvement).
 #       By default maximal number length after dot is 20 (for DEC -> ...).
 # ----------------------------------------------------------------------
 #
 # USAGE:
 #
 #	puts [ NumSystem::hex2dec F.4	]	;# --> 15.25
 #	puts [ NumSystem::hex2oct F.4	]	;# --> 17.2
 #	puts [ NumSystem::hex2bin F.4	]	;# --> 1111.01
 #
 #	puts [ NumSystem::dec2hex 15.25	]	;# --> F.4
 #	puts [ NumSystem::dec2oct 15.25	]	;# --> 17.2
 #	puts [ NumSystem::dec2bin 15.25	]	;# --> 1111.01
 #
 #	puts [ NumSystem::oct2hex 17.2	]	;# --> F.4
 #	puts [ NumSystem::oct2dec 17.2	]	;# --> 15.25
 #	puts [ NumSystem::oct2bin 17.2	]	;# --> 1111.01
 #
 #	puts [ NumSystem::bin2hex 1111.01 ]	;# --> F.4
 #	puts [ NumSystem::bin2dec 1111.01 ]	;# --> 15.25
 #	puts [ NumSystem::bin2oct 1111.01 ]	;# --> 17.2
 #
 #	puts [ NumSystem::ascii2dec @ ]		;# --> 64
 #	puts [ NumSystem::ascii2bin @ ]		;# --> 01000000
 #
 #	puts [ NumSystem::ishex F.4	]	;# --> 1
 #	puts [ NumSystem::isdec 15.25	]	;# --> 1
 #	puts [ NumSystem::isoct 17.2	]	;# --> 1
 #	puts [ NumSystem::isbin 1111.01	]	;# --> 1
 # -----------------------------------------------------------------------

namespace eval NumSystem {

	variable precision	{20}		;# maximal number of digits after dot

	# -----------------------------------------------------------------------
	# NUMERIC SYSTEMS CONVERTIONS
	# -----------------------------------------------------------------------

	# HEX -> ...

	## Hexadecimal -> Decimal
	 # required procedures: `is_X', `hexoct_to_dec', `aux_hexoct_to_dec', `asserthex', `ishex'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc hex2dec {number} {
		return [hexoct_to_dec 16 $number]
	}

	## Hexadecimal -> Octal
	 # required procedures: `is_X', `bin2oct', `hex2bin', `asserthex', `assertbin', `ishex', isbin'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc hex2oct {number} {
		return [bin2oct [hex2bin $number]]
	}

	## Hexadecimal -> Binary
	 # required procedures: `asserthex', `ishex'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc hex2bin {number} {

		# verify value validity
		asserthex $number

		# Number can be negative value
		set sign {}
		if {[string index $number 0] == {-}} {
			set number [string range $number 1 end]
			set sign {-}
		}

		# make it upper-case
		set number [string toupper $number]

		# split value to list of chars
		set number [split $number ""]

		# convert value
		set result {}
		foreach char $number {
			switch $char {
				{0}	{append result {0000}}
				{1}	{append result {0001}}
				{2}	{append result {0010}}
				{3}	{append result {0011}}
				{4}	{append result {0100}}
				{5}	{append result {0101}}
				{6}	{append result {0110}}
				{7}	{append result {0111}}
				{8}	{append result {1000}}
				{9}	{append result {1001}}
				{A}	{append result {1010}}
				{B}	{append result {1011}}
				{C}	{append result {1100}}
				{D}	{append result {1101}}
				{E}	{append result {1110}}
				{F}	{append result {1111}}
				{.}	{append result {.}}
			}
		}

		# return result
		regsub {^0+} $result {} result
		if {[regexp {\.} $result]} {
			regsub {0+$} $result {} result
		}
		regsub {\.$} $result {} result
		regsub {^\.} $result {0.} result
		if {[string length $result] == 0} {
			set result 0
		}
		return $sign$result
	}

	# DEC -> ...

	## Decimal -> Hexadecimal
	 # required procedures: `is_X', `assertdec', isdec'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc dec2hex {number} {
		return [dec_to_X 16 $number]
	}

	## Decimal -> Octal
	 # required procedures: `is_X', `assertdec', isdec'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc dec2oct {number} {
		return [dec_to_X 8 $number]
	}

	## Decimal -> Binary
	 # required procedures: `is_X', `assertdec', `isdec'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc dec2bin {number} {
		return [dec_to_X 2 $number]
	}

	# OCT -> ...

	## Octal -> Hexadecimal
	 # required procedures: `is_X', `bin2hex', `oct2bin', `bin_to_hexoct',
	 #   `aux_hexoct_to_dec', `assertoct', `assertbin',`isbin',`isoct'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc oct2hex {number} {
		return [bin2hex [oct2bin $number]]
	}

	## Octal -> Decimal
	 # required procedures: `is_X', `bin_to_hexoct', `aux_hexoct_to_dec', `assertoct',`isoct'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc oct2dec {number} {
		return [hexoct_to_dec 8 $number]
	}

	## Octal -> Binary
	 # required procedures: `is_X', `assertoct', isoct'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc oct2bin {number} {

		# verify value validity
		assertoct $number

		# Number can be negative value
		set sign {}
		if {[string index $number 0] == {-}} {
			set number [string range $number 1 end]
			set sign {-}
		}

		# split value to list of chars
		set number [split $number ""]

		# convert value
		set result {}
		foreach char $number {
			switch $char {
				{0}	{append result {000}}
				{1}	{append result {001}}
				{2}	{append result {010}}
				{3}	{append result {011}}
				{4}	{append result {100}}
				{5}	{append result {101}}
				{6}	{append result {110}}
				{7}	{append result {111}}
				{.}	{append result {.}}
			}
		}

		# return result
		regsub {^0+} $result {} result
		if {[regexp {\.} $result]} {
			regsub {0+$} $result {} result
		}
		regsub {\.$} $result {} result
		regsub {^\.} $result {0.} result
		if {[string length $result] == 0} {
			set result 0
		}
		return $sign$result
	}

	# BIN -> ...

	## Binary -> Hexadecimal
	 # required procedures: `is_X', `assertbin', isbin', `bin_to_hexoct'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc bin2hex {number} {
		assertbin $number			;# verify value validity
		return [bin_to_hexoct 16 $number]	;# convert value
	}

	## Binary -> Decimal
	 # required procedures: `is_X', `assertbin', isbin'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc bin2dec {number} {

		# verify value validity
		assertdec $number

		# Number can be negative value
		set sign {}
		if {[string index $number 0] == {-}} {
			set number [string range $number 1 end]
			set sign {-}
		}

		# split value to int. part
		regexp {^\d+} $number int

		# split value to frac. part
		if {[regexp {\.\d+$} $number frac]} {
			set frac [string range $frac 1 end]
			set nofrac 0
		} else {
			set frac {}
			set nofrac 1
		}

		# compute int. part
		set tmp [expr [string length $int] -1]
		regexp {^\d+} [expr pow(2,$tmp)] tmp
		set result 0
		foreach value [split $int ""] {
			if {$value} {
				set result [expr {$result+$tmp}]
			}
			set tmp [expr {$tmp / 2}]
			if {$tmp == 0} {break}
		}
		set int $result

		# compute frac. part
		if {!$nofrac} {
			set tmp 0.5
			set result 0
			foreach value [split $frac ""] {
				if {$value} {
					set result [expr {$result+$tmp}]
				}
				set tmp [expr {$tmp / 2}]
			}
			regexp {\d+$} $result frac

			# return converted value with frac.
			return $sign$int.$frac
		}

		# return converted value without frac.
		return $sign$int
	}

	## Binary -> Octal
	 # required procedures: `is_X', `assertbin', isbin', `bin_to_hexoct'
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc bin2oct {number} {
		assertbin $number			;# verify value validity
		return [bin_to_hexoct 8 $number]	;# convert value
	}

	## Ascii char -> Bin
	 # required procedures: (none)
	 # @parm Char number - value to convert
	 # @return mixed - converted value or an empty string
	proc ascii2bin {number} {
		if {[string bytelength $number] != 1} {
			return {}
		}

		set result {}
		scan $number {%c} result
		if {$result != {}} {
			return [dec2bin $result]
		}

		return $result
	}

	## Ascii char -> Dec
	 # required procedures: (none)
	 # @parm Char number - value to convert
	 # @return mixed - converted value or an empty string
	proc ascii2dec {number} {
		if {[string bytelength $number] != 1} {
			return {}
		}
		set result {}
		scan $number {%c} result

		return $result
	}

	# -----------------------------------------------------------------------
	# TYPE ASSERTION
	# -----------------------------------------------------------------------

	## Raise error if the given string is not an hexadecimal value
	 # require procedures: `is_X',`ishex'
	 # @parm String number - string to evaluate
	 # @return mixed - void (failure) or 1 (successful)
	proc asserthex {number} {
		if {![ishex $number]} {
			error "asserthex: Excepted hexadecimal value but got \"$number\""
		} else {
			return 1
		}
	}

	## Raise error if the given string is not an decimal value
	 # require procedures: `is_X',`isdec'
	 # @parm String number - string to evaluate
	 # @return mixed - void (failure) or 1 (successful)
	proc assertdec {number} {
		if {![isdec $number]} {
			error "assertdec: Excepted decimal value but got \"$number\""
		} else {
			return 1
		}
	}

	## Raise error if the given string is not an octal value
	 # require procedures: `is_X',`isoct'
	 # @parm String number - string to evaluate
	 # @return mixed - void (failure) or 1 (successful)
	proc assertoct {number} {
		if {![isoct $number]} {
			error "assertoct: Excepted octal value but got \"$number\""
		} else {
			return 1
		}
	}

	## Raise error if the given string is not an binary value
	 # require procedures: `is_X',`isbin'
	 # @parm String number - string to evaluate
	 # @return mixed - void (failure) or 1 (successful)
	proc assertbin {number} {
		if {![isbin $number]} {
			error "assertbin: Excepted binary value but got \"$number\""
		} else {
			return 1
		}
	}

	# -----------------------------------------------------------------------
	# TYPE CHECKING
	# -----------------------------------------------------------------------

	## Check if the given string can be an Hexadecimal value
	 # require procedure: `is_X'
	 # @parm String number - value to evaluate
	 # @return bool
	proc ishex {number} {
		return [is_X {^[0-9A-Fa-f\.]+$} $number]
	}

	## Check if the given string can be an Decimal value
	 # require procedure: `is_X'
	 # @parm String number - value to evaluate
	 # @return bool
	proc isdec {number} {
		return [is_X {^[0-9\.]+$} $number]
	}

	## Check if the given string can be an Octal value
	 # require procedure: `is_X'
	 # @parm String number - value to evaluate
	 # @return bool
	proc isoct {number} {
		return [is_X {^[0-7\.]+$} $number]
	}

	## Check if the given string can be an Binary value
	 # require procedure: `is_X'
	 # @parm String number - value to evaluate
	 # @return bool
	proc isbin {number} {
		return [is_X {^[01\.]+$} $number]
	}

	# -----------------------------------------------------------------------
	# AUXILIARY PROCEDURES
	# -----------------------------------------------------------------------

	## Auxiliary procedure for convering hex. and oct. to dec.
	 # require procedures: `is_X',`aux_hexoct_to_dec', `assertoct', `asserthex', `ishex', `isoct'
	 # @access PRIVATE
	 # @parm base - source numeric system, posible values are: 8 and 16
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc hexoct_to_dec {base number} {

		# Number can be negative value
		set sign {}
		if {[string index $number 0] == {-}} {
			set number [string range $number 1 end]
			set sign {-}
		}

		# make number upper-case
		set number [string toupper $number]

		# verify value validity
		if {$base == 8} {
			assertoct $number
			set char_len 3
		} else {
			asserthex $number
			set char_len 4
		}

		# split value to int. part
		regexp {^[^\.]+} $number int

		# split value to frac. part
		if {[regexp {\.[^\.]+$} $number frac]} {
			set frac [string range $frac 1 end]
			set nofrac 0
		} else {
			set frac {}
			set nofrac 1
		}

		# compute int. part
		if {$base == 8} {
			set int [expr "0$int"]
		} else {
			set int [expr "0x$int"]
		}

		# compute frac. part
		if {!$nofrac} {
			set frac [aux_hexoct_to_dec [split $frac {}] 1.0 $base]
			regexp {\d+$} $frac frac
			return $sign$int.$frac
		}
		return $sign$int
	}

	## Auxiliary procedure for convering hex. and oct. to dec.
	 # require procedures: none
	 # @access PRIVATE
	 # @parm List vals_list - value to convert splited to a single characters
	 # @parm Number v0 - decimal value of highes bit in the number multipled by 2
	 # @parm base - source numeric system, posible values are: 8 and 16
	 # @return Number - converted value
	proc aux_hexoct_to_dec {vals_list v0 base} {
		set result 0

		foreach char $vals_list {

			if {$base == 8} {
				set v3 $v0
			} else {
				set v3 [expr {$v0 / 2}]
			}
			set v2 [expr {$v3 / 2}]
			set v1 [expr {$v2 / 2}]
			set v0 [expr {$v1 / 2}]

			switch $char {
				{0}	{set bool_map {0 0 0 0}}
				{1}	{set bool_map {0 0 0 1}}
				{2}	{set bool_map {0 0 1 0}}
				{3}	{set bool_map {0 0 1 1}}
				{4}	{set bool_map {0 1 0 0}}
				{5}	{set bool_map {0 1 0 1}}
				{6}	{set bool_map {0 1 1 0}}
				{7}	{set bool_map {0 1 1 1}}
				{8}	{set bool_map {1 0 0 0}}
				{9}	{set bool_map {1 0 0 1}}
				{A}	{set bool_map {1 0 1 0}}
				{B}	{set bool_map {1 0 1 1}}
				{C}	{set bool_map {1 1 0 0}}
				{D}	{set bool_map {1 1 0 1}}
				{E}	{set bool_map {1 1 1 0}}
				{F}	{set bool_map {1 1 1 1}}
			}

			foreach cond $bool_map value "$v3 $v2 $v1 $v0" {
				if {$cond} {
					set result [expr {$result+$value}]
				}
			}
		}
		return $result
	}

	## Auxiliary procedure for convering bin. to hex. and oct.
	 # require procedures: none
	 # @access PRIVATE
	 # @parm Int base - target numeric system, posible values are: 8 and 16
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc bin_to_hexoct {base number} {

		# Number can be negative value
		set sign {}
		if {[string index $number 0] == {-}} {
			set number [string range $number 1 end]
			set sign {-}
		}

		# set some essential variables
		if {$base == 8} {
			set modulo 3
			set mod_1 2
			set padding {}
			set convCmd {oct_to_bin}
		} else {
			set modulo 4
			set mod_1 3
			set padding {0}
			set convCmd {hex_to_bin}
		}

		# split value to int. and frac. part
		regexp {^\d+} $number int
		if {[regexp {\.\d+$} $number frac]} {
			set frac [string range $frac 1 end]
			set nofrac 0
		} else {
			set frac {}
			set nofrac 1
		}

		# convert int
		set result {}

		set length [string length $int]
		set length [expr {($length % $modulo) - 1}]
		if {$length >= 0} {
			set firstvalue [string range $int 0 $length]
			set int [string range $int [expr {$length + 1}] end]

			switch $length {
				{0}	{set firstvalue "${padding}00$firstvalue"}
				{1}	{set firstvalue "${padding}0$firstvalue"}
				{2}	{set firstvalue "${padding}$firstvalue"}
			}

			lappend result $firstvalue
		}

		while {$int != ""} {
			lappend result [string range $int 0 $mod_1]
			set int [string range $int $modulo end]
		}

		set int [$convCmd $result]
		regsub {^0+} $int {} int
		if {$int == {}} {set int 0}

		# convert frac
		set result {}
		if {!$nofrac} {
			# make list
			set idx -1
			while {$frac != ""} {
				lappend result [string range $frac 0 $mod_1]
				set frac [string range $frac $modulo end]
				incr idx
			}

			set lastValue [lindex $result $idx]
			switch [string length $lastValue] {
				{1}	{lset result $idx "${lastValue}${padding}00"}
				{2}	{lset result $idx "${lastValue}${padding}0"}
				{3}	{lset result $idx "${lastValue}${padding}"}
			}

			set frac [$convCmd $result]
			regsub {0+$} $frac {} frac
			if {$frac == {}} {set frac 0}

			# return converted value with frac.
			return $sign$int.$frac
		}

		# return converted value without frac.
		return $sign$int
	}

	## Auxiliary procedure for convering dec to hex, oct, bin
	 # require procedures: `is_X', `assertdec', `isdec'
	 # @access PRIVATE
	 # @parm Int base - target numeric system, posible values are: 2, 8, 10, 16
	 # @parm Number number - value to convert
	 # @return Number - converted value
	proc dec_to_X {base number} {
		variable precision

		# verify values validity
		if {!($base==16 || $base==10 || $base==8 || $base==2)} {
			error "dec_to_X: Unrecognized numeric system \"$base\". Possible values are: 2, 8, 10, 16"
		}
		assertdec $number

		# Number can be negative value
		set sign {}
		if {[string index $number 0] == {-}} {
			set number [string range $number 1 end]
			set sign {-}
		}

		# split value to int. and frac. part
		regexp {^\d+} $number int
		if {[regexp {\.\d+$} $number frac]} {
			set frac [string range $frac 1 end]
			set nofrac 0
		} else {
			set frac {}
			set nofrac 1
		}

		if {[string length $int] > 12} {
			error "Unable to convert, value is too high"
		}

		# convert integer part
		set reminder $int
		set int ""
		while {$reminder > 0} {
			set tmp [expr {$reminder % $base}]
			if {$base == 16} {
				switch $tmp {
					10	{set tmp A}
					11	{set tmp B}
					12	{set tmp C}
					13	{set tmp D}
					14	{set tmp E}
					15	{set tmp F}
				}
			}
			set int ${tmp}${int}
			regexp {^\d+} [expr {$reminder / $base}] reminder
		}
		if {$int == {}} {set int 0}

		# convert frac. part
		if {!$nofrac} {
			set reminder "0.$frac"
			set frac ""
			for {set i 0} {$i < $precision} {incr i} {
				set reminder [expr {$reminder * $base}]
				regexp {^\d+} $reminder tmp
				set reminder [expr {$reminder - $tmp}]
				if {$base == 16} {
					switch $tmp {
						10	{set tmp A}
						11	{set tmp B}
						12	{set tmp C}
						13	{set tmp D}
						14	{set tmp E}
						15	{set tmp F}
					}
				}
				append frac $tmp
				if {$reminder == 0} {break}
			}
			if {$frac == {}} {set frac 0}

			# return converted value with frac.
			return $sign$int.$frac
		}

		# return converted value without frac.
		return $sign$int
	}

	## Auxiliary procedure for convering oct to bin
	 # require procedures: none
	 # @access PRIVATE
	 # @parm List vals_list - value to convert splited to single characters
	 # @return Number - converted value
	proc oct_to_bin {vals_list} {

		# iterate over items in list and traslate them
		set result ""
		foreach char $vals_list {
			# convert item
			switch $char {
				{000}	{append result 0}
				{001}	{append result 1}
				{010}	{append result 2}
				{011}	{append result 3}
				{100}	{append result 4}
				{101}	{append result 5}
				{110}	{append result 6}
				{111}	{append result 7}
			}
		}

		# done
		return $result
	}

	## Auxiliary procedure for convering hex to bin
	 # require procedures: none
	 # @access PRIVATE
	 # @parm List vals_list - value to convert splited to single characters
	 # @return Number - converted value
	proc hex_to_bin {vals_list} {

		# iterate over items in list and traslate them
		set result ""
		foreach char $vals_list {
			# convert item
			switch $char {
				{0000}	{append result 0}
				{0001}	{append result 1}
				{0010}	{append result 2}
				{0011}	{append result 3}
				{0100}	{append result 4}
				{0101}	{append result 5}
				{0110}	{append result 6}
				{0111}	{append result 7}
				{1000}	{append result 8}
				{1001}	{append result 9}
				{1010}	{append result A}
				{1011}	{append result B}
				{1100}	{append result C}
				{1101}	{append result D}
				{1110}	{append result E}
				{1111}	{append result F}
			}
		}

		# done
		return $result
	}

	## Auxiliary procedure for num. checking
	 # Check if the given string contain 0 or 1 dot and match the given
	 # regular expression
	 # @access PRIVATE
	 # @parm String regexpr - reg. exp. of allowed symbols
	 # @parm String number - string to evaluate
	 # return bool
	proc is_X {regexpr number} {

		# The given number can begin with minus sign
		if {[string index $number 0] == {-}} {
			set number [string range $number 1 end]
		}

		# 1st condition (check for allowed symbols)
		if {![regexp $regexpr $number]} {
			return 0
		}

		# 2nd condition (must contain maximaly one dot)
		set cnd1 [split $number {\.}]
		if {[llength $cnd1] > 2} {
			return 0
		}

		# 3rd condition (dot must not be at the beginning or end)
		if {[regexp {^\.} $number]} {return 0}
		if {[regexp {\.$} $number]} {return 0}

		# return result
		return 1
	}
}


## ----------------------------------------------------------------------
## Converts between angle units and normalizes angle values
 #
 # Supported angle units: rad, deg, grad
 # note: all converted angles are normalized before convertion
 # -----------------------------------------------------------------------
 #
 # USAGE:
 #
 #	puts [ Angle::adjustAngle deg -700 ]	;# --> 20.0000000016 (should be exactly 20)
 #
 #	puts [ Angle::rad2deg	$Angle::PI]	;# --> 180.0
 #	puts [ Angle::rad2grad	$Angle::PI]	;# --> 200.0
 #
 #	puts [ Angle::deg2rad	180	]	;# --> 3.141592654
 #	puts [ Angle::deg2grad	180	]	;# --> 200.0
 #
 #	puts [ Angle::grad2deg	200	]	;# --> 180.0
 #	puts [ Angle::grad2rad	200	]	;# --> 3.141592654
 #
 #	puts $Angle::PI				;# --> 3.141592654
 # -----------------------------------------------------------------------

namespace eval Angle {

	variable PI		{3.141592654}	;# Pi

	# CONVERSION OF ANGLE VALUES
	# --------------------------

	## Radians -> Degrees
	 # require procedure: `adjustAngle'
	 # @parm Number angle - angle value to convert
	 # @return Nubmber - converted value
	proc rad2deg {angle} {
		variable PI

		set angle [adjustAngle rad $angle]
		return [expr {(180 / $PI) * $angle}]
	}

	## Radians -> GRAD
	 # require procedure: `adjustAngle'
	 # @parm Number angle - angle value to convert
	 # @return Nubmber - converted value
	proc rad2grad {angle} {
		variable PI

		set angle [adjustAngle rad $angle]
		return [expr {(200 / $PI) * $angle}]
	}

	## Degrees -> Radians
	 # require procedure: `adjustAngle'
	 # @parm Number angle - angle value to convert
	 # @return Nubmber - converted value
	proc deg2rad {angle} {
		variable PI

		set angle [adjustAngle deg $angle]
		return [expr {($PI / 180) * $angle}]
	}

	## Degrees -> Radians
	 # require procedure: `adjustAngle'
	 # @parm Number angle - angle value to convert
	 # @return Nubmber - converted value
	proc deg2grad {angle} {
		set angle [adjustAngle deg $angle]
		return [expr {(10 / 9.0) * $angle}]
	}

	## GRAD -> Degrees
	 # require procedure: `adjustAngle'
	 # @parm Number angle - angle value to convert
	 # @return Nubmber - converted value
	proc grad2deg {angle} {
		set angle [adjustAngle grad $angle]
		return [expr {0.9 * $angle}]
	}

	## GRAD -> Radians
	 # require procedure: `adjustAngle'
	 # @parm Number angle - angle value to convert
	 # @return Nubmber - converted value
	proc grad2rad {angle} {
		variable PI

		set angle [adjustAngle grad $angle]
		return [expr {($PI / 200) * $angle}]
	}

	## Ajust angle value and polarity
	 # @parm String unit - unit of angle (rad | deg | grad)
	 # @parm angle angle - value of angle
	 # @return angle - adjusted angle value
	proc adjustAngle {unit angle} {
		variable PI

		# verify if the given angle is a valid number
		if {![regexp {^\-?\d+(\.\d+)?$} $angle]} {
			error "adjustAngle: Excepted integer or float but got \"$angle\""
		}

		# determinate base for division
		switch $unit {
			{rad}	{set base [expr {$PI * 2}]}
			{deg}	{set base 360.0}
			{grad}	{set base 400.0}
			default	{error "Unrecognized option \"$unit\""}
		}

		# is negative or something else ?
		if {$angle < 0} {
			set minus 1
		} else {
			set minus 0
		}

		# adjust angle value
		set angle [expr {$angle / $base}]
		regsub {^[-]?\d+} $angle {0} angle
		set angle [expr {$angle * $base}]

		# adjust angle polarity
		if {$minus} {return [expr {$base - $angle}]}
		return $angle
	}
}

# >>> File inclusion guard
}
# <<< File inclusion guard