r108363 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108362‎ | r108363 | r108364 >
Date:20:15, 8 January 2012
Author:brion
Status:resolved (Comments)
Tags:
Comment:
* 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) (history)

Diff [purge]

Index: trunk/phase3/languages/Language.php
@@ -3816,28 +3816,29 @@
38173817 * @return string
38183818 */
38193819 function formatBitrate( $bps ) {
3820 - $units = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
38213820 if ( $bps <= 0 ) {
38223821 return str_replace( '$1', $this->formatNum( $bps ), $this->getMessageFromDB( 'bitrate-bits' ) );
38233822 }
3824 - $unitIndex = (int)floor( log10( $bps ) / 3 );
3825 - $mantissa = $bps / pow( 1000, $unitIndex );
 3823+ $units = array( '', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zeta', 'yotta' );
 3824+ $index = 0;
38263825
38273826 $maxIndex = count( $units ) - 1;
 3827+ while ( $bps >= 1000 && $index < $maxIndex ) {
 3828+ $index++;
 3829+ $bps /= 1000;
 3830+ }
38283831
3829 - if ( $unitIndex > $maxIndex ) {
3830 - // Prevent code falling off end of $units array
3831 - $mantissa *= ( $unitIndex - $maxIndex ) * 1000;
3832 - $unitIndex = $maxIndex;
 3832+ // For small units no decimal places necessary
 3833+ $round = 0;
 3834+ if ( $index > 1 ) {
 3835+ // For MB and bigger two decimal places are smarter
 3836+ $round = 2;
38333837 }
3834 - if ( $mantissa < 10 ) {
3835 - $mantissa = round( $mantissa, 1 );
3836 - } else {
3837 - $mantissa = round( $mantissa );
3838 - }
3839 - $msg = "bitrate-{$units[$unitIndex]}bits";
 3838+ $msg = "bitrate-{$units[$index]}bits";
 3839+
 3840+ $bps = round( $bps, $round );
38403841 $text = $this->getMessageFromDB( $msg );
3841 - return str_replace( '$1', $this->formatNum( $mantissa ), $text );
 3842+ return str_replace( '$1', $this->formatNum( $bps ), $text );
38423843 }
38433844
38443845 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r108364Followup r108363 for bug 33571...reedy20:40, 8 January 2012

Comments

#Comment by Reedy (talk | contribs)   20:28, 8 January 2012

I was thinking about normalising the code when I was adding to it...

The difference between the 2 blocks of code is 1000 vs 1024, and then different message keys, so could refactor most of it out...

#Comment by Hashar (talk | contribs)   14:05, 9 January 2012

What should '999999' gives in kbps ?

  • 999kbps
  • 999,999kbps
  • 1,000kbps

I find the later acceptable.


Thanks brion for the fix!

Status & tagging log