LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2009, 06:33 AM   #1
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
[php]sprintf issue that I don't understand


The following code calculates a percentage for an uptime
Code:
    $uptime=100 * ($total['runtime'] - $total['downtime']) / $total['runtime'];
    debug_print($uptime);
    $uptime=sprintf("%2.2f", $uptime);
    echo $uptime;
The output for a certain calculation is 99.996136186824 (for the debug_print) and 100.00 for the echo. I expected the latter to output 99.99.

Is my understanding of the sprintf precision specifier wrong? I thought it would simply chop the last digits off, but it seems to round first.

PHP version 4.3.10
 
Old 01-15-2009, 07:38 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
It is the same in C.

Maybe not what you expected, but it is also scientifically correct. If you specify the precision of a number in 2 decimals, the number has an uncertainty of 0.005.

Which means:

100.00 can be: 99.995 < value < 100.005 (which 99.9961.... is) This is scientifically correct.

If your output would have been 99.99, the original number could have been: 99.985 < value < 99.995 which is not correct.

So the precision specifier does exactly what the name says: it specifies the precision, not the number of decimal places. Yes, many text writers cut some corners and say it is the number of decimal places. If that were true, 99.9961... would be displayed as 99.99. But is does not correctly reflect the value that you measured.

(On a side note: it is therefore nonsense for a digital multimeter to display 1.534 as battery voltage if the meter has 1% accuracy)

If your display is 100.00, you can be sure the availability was > 99.995%. If you want to show that it was less than 100%, you have to use a higher precision.

jlinkels
 
Old 01-15-2009, 08:09 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If you always want to floor it then try...
PHP Code:
echo floor($uptime*100)/100
 
Old 01-15-2009, 10:57 PM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Original Poster
Rep: Reputation: 282Reputation: 282Reputation: 282
Thanks

I know it's mathematically correct, but did not expect it. As a programmer I want it mathematically correct, as a user I don't expect to see an uptime of 100% if there was the smallest amount of downtime. I will talk to the customer (again).

And I indeed used a floor to solve the issue.

Thanks again

Last edited by Wim Sturkenboom; 01-15-2009 at 11:04 PM.
 
  


Reply

Tags
php, precision



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sprintf() help needed!!! rino.caldelli Programming 9 03-07-2006 06:43 AM
sprintf to c++ conversion rino.caldelli Programming 2 03-03-2006 09:44 PM
sprintf value disappear syseeker Programming 8 02-17-2006 04:20 AM
Getting php to understand that mysql exists Red Squirrel Linux - Software 2 01-18-2006 01:21 AM
I dont understand PHP includes logicdisaster Programming 2 04-20-2005 04:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration