| View previous topic :: View next topic |
| Author |
Message |
theravenz Voice
Joined: 18 Jan 2007 Posts: 7
|
Posted: Thu Jan 18, 2007 9:58 pm Post subject: TCL IRC Output.. Changing Bytes to KB, MB, ect.. |
|
|
Just having some trouble with this coding..
Searched everywhere for help.. hopfully one of you guys can help me out
It'd be much appriciated.
| Code: | if {[expr $upload / 1024] >= 1} {
set uploaded "[string range "[expr $upload / 1024.0]" 0 [expr [string length "[expr $upload / 1024]"]+ 2] ] KB"};
if {[expr $upload / 1048576] >= 1} {
set uploaded "[string range "[expr $upload / 1048576.0]" 0 [expr [string length "[expr $upload / 1048576]"]+ 2] ] MB"};
if {[expr $upload / 1073741824] >= 1} {
set uploaded "[string range "[expr $upload / 1073741824.0]" 0 [expr [string length "[expr $upload / 1073741824]"]+ 2] ] GB"};
if {[expr $upload / 1099511627776] >= 1} {
set uploaded "[string range "[expr $upload / 1099511627776.0]" 0 [expr [string length "[expr $upload / 1099511627776]"]+ 2] ] TB"};
if {[expr $download / 1024] >= 1} {
set downloaded "[string range "[expr $download / 1024.0]" 0 [expr [string length "[expr $download / 1024]"]+ 2] ] KB"};
if {[expr $download / 1048576] >= 1} {
set downloaded "[string range "[expr $download / 1048576.0]" 0 [expr [string length "[expr $download / 1048576]"]+ 2] ] MB"};
if {[expr $download / 1073741824] >= 1} {
set downloaded "[string range "[expr $download / 1073741824.0]" 0 [expr [string length "[expr $download / 1073741824]"]+ 2] ] GB"};
if {[expr $download / 1099511627776] >= 1} {
set downloaded "[string range "[expr $download / 1099511627776.0]" 0 [expr [string length "[expr $download / 1099511627776]"]+ 2] ] TB"}; |
now im not fantastic when it comes to TCL more of a novice..
When these lines are called.. i get an error like this..
| Quote: | | [12:53] <ServBot> [12:53] Tcl error [user_search]: integer value too large to represent |
I've isolated it to these lines only.. if i comment them out.. it works flawlessly.. cept displayed in bytes
One other thing i noticed is, if i comment out the GB and TB lines and leave KB and MB.. it works for anything under a Gig..
anything over a gig it just returns that error again
Any help would be nice.. thanks in advance..
Matt |
|
| Back to top |
|
 |
rosc2112 Revered One

Joined: 19 Feb 2006 Posts: 1454 Location: Northeast Pennsylvania
|
Posted: Fri Jan 19, 2007 6:04 am Post subject: |
|
|
| Why don't you use elseif instead, so that the largest numbers are not being divided by the smallest divisor (I'm assuming that is the problem, since you didn't bother to post the actual '.set errorInfo' debug.. Could be the opposit, smallest number being divided by the largest divisor..) |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Fri Jan 19, 2007 7:25 am Post subject: |
|
|
You'll get an overflow for integers >= 1<<31 and that error you mentioned for integers >= 1<<32... Try this proc:
| Code: | proc bytesToHumanReadable b {
set b [format %g $b]
foreach {v n} {1.09951e+012 TB 1.07374e+009 GB 1.04858e+006 MB 1024.0 KB 1.0 B} {
if {$b>=$v} break
}
format "%.2f %s" [expr {$b/$v}] $n
} |
EDIT: oops  _________________ Have you ever read "The Manual"?
Last edited by user on Fri Jan 19, 2007 7:29 am; edited 2 times in total |
|
| Back to top |
|
 |
theravenz Voice
Joined: 18 Jan 2007 Posts: 7
|
Posted: Fri Jan 19, 2007 7:25 am Post subject: |
|
|
Even with elseif.. -- thought i had tried that before posting it.. and it didnt seem to like elseif.. but oh wells works now...
Still no go.. comes up with
[22:23] <ServBot> [22:22] Tcl error [user_search]: integer value too large to represent
..
Could it be that TCL just cant handle calculations with such large numbers???
coz thats the only thing i can figure out
Would anyone know of any other code that might be able to help with this calulation that i could try?
PS, new post while posting this.. ill give it a go dude |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Fri Jan 19, 2007 7:33 am Post subject: |
|
|
Make sure you try the edited version. The first proc i posted had an error for values < 1kb  _________________ Have you ever read "The Manual"? |
|
| Back to top |
|
 |
theravenz Voice
Joined: 18 Jan 2007 Posts: 7
|
Posted: Fri Jan 19, 2007 8:40 am Post subject: |
|
|
So how am i calling this proc?
do i just do something like
set uploaded bytesToHumanReadable($upload); |
|
| Back to top |
|
 |
theravenz Voice
Joined: 18 Jan 2007 Posts: 7
|
Posted: Fri Jan 19, 2007 8:52 am Post subject: |
|
|
Worked it out.. thanks heaps man!!!
You're a Champ! |
|
| Back to top |
|
 |
theravenz Voice
Joined: 18 Jan 2007 Posts: 7
|
Posted: Sat Jan 20, 2007 1:22 pm Post subject: |
|
|
Okay, i got a new issue..
I totally dont understand that script you wrote.. hah! my TCL Noobness..
Anyways im just after a calc for ratio..
need to divide $upload by $download to give a 1.000 count..
Say 4 / 2 = 2.000 (to 3 decimal places.)
What im also after to.. is if they havent downloaded it returns "Inf." and if they havent uploaded it returns "---"
Im sure i could code that in if i only got the ratio calc...
If anyone knows how to calc big numbers in this way i would appriciate your help.. I can calc anything up to MB sized numbers in bytes.. but soon as i get up to GB sized numbers in bytes i get that error i was experiencing earlier.. |
|
| Back to top |
|
 |
user

Joined: 18 Mar 2003 Posts: 1452 Location: Norway
|
Posted: Sat Jan 20, 2007 5:46 pm Post subject: |
|
|
| Code: | proc ratio {up down} {
format %.3f [expr {[format %g $up]/[format %g $down]}]
} |
Keep in mind that this (and the previous) proc will not give you the exact answer, but it's the best you can do without loading external libraries. _________________ Have you ever read "The Manual"? |
|
| Back to top |
|
 |
theravenz Voice
Joined: 18 Jan 2007 Posts: 7
|
Posted: Sun Jan 21, 2007 2:13 am Post subject: |
|
|
cheers man
Yeah im not really after 100% values just aprox so its all good.
thanks again... champ |
|
| Back to top |
|
 |
|