https://www.mediawiki.org/wiki/Special:Code/MediaWiki/108363

Revision: 108363
Author:   brion
Date:     2012-01-08 20:15:08 +0000 (Sun, 08 Jan 2012)
Log Message:
-----------
* bug 33571: fix yottabits/s in Language::formatBitrate

Problem was caused by inexact floating-point comparisons with values returned 
from
log10(); worked around by simply duplicating the very similar code in the 
function
immediately below, which does the same thing with 1024 instead of 1000 unit 
sizes,
uses only simple division, and passes the test cases.

Modified Paths:
--------------
    trunk/phase3/languages/Language.php

Modified: trunk/phase3/languages/Language.php
===================================================================
--- trunk/phase3/languages/Language.php 2012-01-08 18:12:23 UTC (rev 108362)
+++ trunk/phase3/languages/Language.php 2012-01-08 20:15:08 UTC (rev 108363)
@@ -3816,28 +3816,29 @@
         * @return string
         */
        function formatBitrate( $bps ) {
-               $units = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 
'exa', 'zeta', 'yotta' );
                if ( $bps <= 0 ) {
                        return str_replace( '$1', $this->formatNum( $bps ), 
$this->getMessageFromDB( 'bitrate-bits' ) );
                }
-               $unitIndex = (int)floor( log10( $bps ) / 3 );
-               $mantissa = $bps / pow( 1000, $unitIndex );
+               $units = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 
'exa', 'zeta', 'yotta' );
+               $index = 0;
 
                $maxIndex = count( $units ) - 1;
+               while ( $bps >= 1000 && $index < $maxIndex ) {
+                       $index++;
+                       $bps /= 1000;
+               }
 
-               if ( $unitIndex  > $maxIndex ) {
-                       // Prevent code falling off end of $units array
-                       $mantissa *= ( $unitIndex - $maxIndex ) * 1000;
-                       $unitIndex = $maxIndex;
+               // For small units no decimal places necessary
+               $round = 0;
+               if ( $index > 1 ) {
+                       // For MB and bigger two decimal places are smarter
+                       $round = 2;
                }
-               if ( $mantissa < 10 ) {
-                       $mantissa = round( $mantissa, 1 );
-               } else {
-                       $mantissa = round( $mantissa );
-               }
-               $msg = "bitrate-{$units[$unitIndex]}bits";
+               $msg = "bitrate-{$units[$index]}bits";
+
+               $bps = round( $bps, $round );
                $text = $this->getMessageFromDB( $msg );
-               return str_replace( '$1', $this->formatNum( $mantissa ), $text 
);
+               return str_replace( '$1', $this->formatNum( $bps ), $text );
        }
 
        /**


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to