scientific notation of double/float

Topics: Developer Forum, Project Management Forum, User Forum
Feb 16, 2009 at 2:14 PM
The problem is the following:
We are creating an instance of a class testObject and fill a var with a double.

A class TestExcel extends from PhpExcel and when submitting the object testObject into the constructor we get a scientific notation of the var when we do a var_dump.

Can anyone help us out. My colleagues and I don't understand how it is possible that the content of an instance can be changed by extending the phpExcel class.

When we don't do the extension, the var_dump returns the correct notation. -- 200000
When doing the extension we get the scientific notation -- 2.0E+5

[code]

// instance of the testObject

$number = new testObject();
$number->setNumber((double)200000);
      
// contruct testExcelClass
$excel = new TestExcel($number);
      

// Class TestObject      
class testObject {

      private $number;

      public function setNumber($number){
          $this->number = $number;
      }
      
      public function getNumber(){
          return $this->number;
      }
      
}
 
// class test excell extends from phpexcel  
class TestExcel extends PHPExcel  {
 
    private $number;
    
    public function __construct($number){
      parent::__construct();
      
      $this->number = $number;
      print_r($this->number); exit();
      
    }
 
}
[/code]

Coordinator
Feb 16, 2009 at 3:19 PM
Using your classes, and the following code:
[code]
$number = new testObject();
$number->setNumber((double)200000);
print_r($number->getNumber());
echo '<hr />';
// contruct testExcelClass
$excel = new TestExcel($number);
[/code]

I'm getting
[code]
2.0E+5
---
testObject Object ( [number:private] => 2.0E+5 )
[/code]
which is what I'd expect to see


If I modify the testExcel class to not extend PHPExcel
[code]
class TestExcel  {
    private $number;
    public function __construct($number){
      $this->number = $number;
      print_r($this->number); exit();
    }
}
[/code]
I get exactly the same result

And I get the value displayed in scientific notation if I simply do
[code]
echo (float)200000;
[/code]

However, I get an unformatted display if I do
[code]
echo (float)234567;
[/code]

Feb 16, 2009 at 4:00 PM
the last example is something we got to.
Indeed echo (float)200000 does give the scientific notation 2.0 E+5
and echo (float)234567; does indeed give the full number but this isn't the answer to my problem.
That you get the same result with and without the phpexcel class is even a bigger mystery.
The var_dump of the variable in a class without phpexcel gives us a full number, with phpexcel it gets the scientific notation....
Developer
Feb 16, 2009 at 7:20 PM
Yeah, this is some annoying behavior of PHP which was introduced unintentionally, it seems, and arguably a bug
http://bugs.php.net/bug.php?id=43053

Looks like this will be fixed in PHP 5.3.0.

Feb 17, 2009 at 8:42 AM
The mistery becomes even bigger:
There might be a bug in PHP like Koyama said but then we don't get the following issue.

when the TestExcel class above is changed to this:

[code]
class TestExcel extends emptyClass {
    private $number;
    
    public function __construct($number){
      
      $this->number = $number;
      print_r($this->number);       
      exit();
      
    }
}
[/code]

where emptyClass is in fact an empty class ( how brilliant is that?)
[code]
class emptyClass {
    
}
[/code]

this doesn't give an issue and the ouput is: 200000

when the extendclass is PHPExcel the output is: 2.0 E+5

In one or another way PHPExcel must have an influance on this variable......
Any other ideas?
Developer
Feb 17, 2009 at 5:50 PM
Edited Feb 17, 2009 at 5:50 PM
When I try this:

echo (float)200000;

I am apparently now get this output using PHP 5.2.8 / Linux

200000

Previously, on other versions I would get 2.0 E+5. I can't seem to reproduce now, but there is some weirdness going on here and it shows that results depend on your PHP version / installation. I can't imagine how PHPExcel can influence on this.

But can you post your scripts, in their full length, with and without extending PHPExcel. We can then copy-paste and test it on a few servers and see what we get.

Feb 18, 2009 at 8:53 AM
You were right with your first answer that this is a bug in php 5.2.6 ( maybe previous versions) but it has been fixed in 5.2.8

so we did the ugrade and this works correct now.

The only thing is that we ( and you to :-) ) still don't understand why the extend with the PHPExcel class changes the notation of the number.
I also cannot give you more code because the code i gave is the one it does not work with.

so you get two classes to work with

class TestExcel {
    private $number;
    
    public function __construct($number){
      
      $this->number = $number;
      print_r($this->number);       
      exit();
      
    }
}

this outputs 200000

class TestExcel extends PHPExcel{
    private $number;
    
    public function __construct($number){
      
      $this->number = $number;
      print_r($this->number);       
      exit();
      
    }
}

this outputs 2.0 E+5

after all the problem has been solved, but we don't know what the solution was.... (without the update off course)

thank you guys for the follow up of this issue.
best regards

Developer
Feb 18, 2009 at 10:23 PM
Found it!

Apparently, echoing the float gives me 2.0E+5 on PHP 5.2.6, but only when precision is 15 or higher. On PHP 5.2.8, I never get 2.0E+5, but always 200000, it seems. Examples:

Trying on PHP Version 5.2.6-pl7-gentoo

ini_set('precision', 14);
echo (float)200000; // gives 200000

ini_set('precision', 15);
echo (float)200000; // gives 2.0E+5

Trying on PHP Version 5.2.8-0.dotdeb.1

ini_set('precision', 14);
echo (float)200000; // gives 200000

ini_set('precision', 15);
echo (float)200000; // gives 200000

PHPExcel is effectively setting precision to 15 in PHExcel/Calculation/Functions.php. This explains behavior when extending PHPExcel.

$savedPrecision = ini_get('precision');
if ($savedPrecision < 15) {
   ini_set('precision',15);
}

Upgrading from PHP 5.2.6 to 5.2.8 is the solution. I consider this a PHP bug. Thank you for posting your findings.