egghelp.org community Forum Index
[ egghelp.org home | forum home ]
egghelp.org community
Discussion of eggdrop bots, shell accounts and tcl scripts.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problem with formating units

 
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help
View previous topic :: View next topic  
Author Message
aRTiST
Voice


Joined: 17 Apr 2009
Posts: 4

PostPosted: Sat Apr 18, 2009 1:58 pm    Post subject: Problem with formating units Reply with quote

Code:
proc format_1024_units {value} {
   set len [string length $value]
   if {$value < 1024} {
      return [format "%s B" $value]
   } else {
      set unit [expr {($len - 1) / 3}]
      return [format "%.1f %s" [expr {$value / pow(1024,$unit)}] [lindex [list B KB MB GB TB PB EB ZB YB] $unit]]
   }
}


But the result is then often 0.0TB (value is really 35.2GB unformated).

Any hints where im doing wrong ?

TIA arti
Back to top
View user's profile Send private message
speechles
Revered One


Joined: 26 Aug 2006
Posts: 1398
Location: emerald triangle, california (coastal redwoods)

PostPosted: Sat Apr 18, 2009 11:22 pm    Post subject: Reply with quote

The problem here is your method of attaining size doesn't match throughout your procedure. You initially set "len" to the [string length $value]. Then you set "unit" to to "($len-1) / 3". This where you make your mistake. This assumes 1,000 for our divisor. Yet below that you make use of the correct 1,024 to raise powers which throws off the entire equation. Using the method below should solve your problem and its more compact.

Code:
proc format_1024_units {value} {
   set test $value ; set unit 0
   while {[set test [expr {$test / 1024}]] > 0} {
      incr unit
   }
   return [format "%.1f %s" [expr {$value / pow(1024,$unit)}] [lindex [list B KB MB GB TB PB EB ZB YB] $unit]]
}

_________________
speechles' eggdrop tcl archive
Back to top
View user's profile Send private message
aRTiST
Voice


Joined: 17 Apr 2009
Posts: 4

PostPosted: Sun Apr 19, 2009 5:25 am    Post subject: hm Reply with quote

I tried with ur version, but now ill get an error :

Code:
floating-point value too large to represent


:/
Back to top
View user's profile Send private message
nml375
Revered One


Joined: 04 Aug 2006
Posts: 2857

PostPosted: Sun Apr 19, 2009 9:28 am    Post subject: Reply with quote

Could you verify that $value is indeed an integer and not a float?

The code posted by speechles relies on integer division, and will fail if either the nominator or the denominator is a float. I'd also recommend adding a constraint to the value of unit, as to prevent it from going out of bounds for the "suffix-list".
You might be able to get away with forcing $test to an integer in the beginning.
_________________
NML_375, idling at #eggdrop@IrcNET
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    egghelp.org community Forum Index -> Scripting Help All times are GMT - 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Forum hosting provided by Reverse.net

Powered by phpBB © 2001, 2005 phpBB Group
subGreen style by ktauber