php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #1029 printf (& sprintf) dosn't handle exponential format for floats correctly
Submitted: 1999-01-05 06:29 UTC Modified: 2002-06-15 09:20 UTC
From: leif dot thuresson at tsl dot uu dot se Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: leif dot thuresson at tsl dot uu dot se
New email:
PHP Version: OS:

 

 [1999-01-05 06:29 UTC] leif dot thuresson at tsl dot uu dot se
System: Linux, Apache with PHP-module
Problem: printf (& sprintf) dosn't handle exponential format for floats correctly

Comment: This isn't actually a bug since it isn't documented to work in the manual.
                I nedded exponential format in my application and since the function
                "formatted_print.c" already had some support for exp. format (but
                not working correctly) the simplest solution was to fix it in the php source code.
                Below is my patch to  "formatted_print.c" if you would like to include it 
                in a future rease of PHP.
                Thanks for a great tool for building web interfaces to databases !

                Regards,
                   -leif



*** formatted_print.c.org	Mon Jan  4 21:06:38 1999
--- formatted_print.c	Mon Jan  4 21:13:09 1999
***************
*** 257,263 ****
  	} else if (precision > MAX_FLOAT_PRECISION) {
  		precision = MAX_FLOAT_PRECISION;
  	}
! 	cvt = _php3_cvt(number, precision, &decpt, &sign, (fmt == 'e'));
  
  	if (sign) {
  		numbuf[i++] = '-';
--- 257,266 ----
  	} else if (precision > MAX_FLOAT_PRECISION) {
  		precision = MAX_FLOAT_PRECISION;
  	}
! 	if (fmt == 'e')
! 		cvt = _php3_cvt(number, precision+1, &decpt, &sign, 1);
! 	else
! 		cvt = _php3_cvt(number, precision, &decpt, &sign, 0);
  
  	if (sign) {
  		numbuf[i++] = '-';
***************
*** 288,294 ****
  	while (cvt[j]) {
  		numbuf[i++] = cvt[j++];
  	}
! 
  	numbuf[i] = '\0';
  
  	if (precision > 0) {
--- 291,311 ----
  	while (cvt[j]) {
  		numbuf[i++] = cvt[j++];
  	}
! 	if (fmt == 'e')	{
! 		int exponent = decpt-1;
! 		numbuf[i++] = 'e';
! 		if (exponent < 0)
! 			numbuf[i++] = '-';
! 		else
! 			numbuf[i++] = '+';
! 		exponent = abs(exponent);
! 		if (exponent > 99)	{
! 			numbuf[i++] = '0' + exponent / 100;
! 			exponent = exponent % 100;
! 		}
! 		numbuf[i++] = '0' + exponent / 10;
! 		numbuf[i++] = '0' + exponent % 10;
! 	}
  	numbuf[i] = '\0';
  
  	if (precision > 0) {

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-11-12 11:31 UTC] joey at cvs dot php dot net
Moving to feature req.
 [2001-02-10 12:55 UTC] jimw@php.net
really reassigned to 4.0 this time.
 [2001-07-22 12:28 UTC] andy@php.net
Should this request be closed?  It's really old...
 [2002-06-15 09:20 UTC] derick@php.net
I've no clue what this is about... printf seems to work just fine here with exponentional numbers.

Derick
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed May 07 19:01:32 2025 UTC