php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #32434 Bad result for float multiplication
Submitted: 2005-03-23 21:11 UTC Modified: 2005-03-24 18:08 UTC
From: vblavet@php.net Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.10 OS: Windows XP
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: vblavet@php.net
New email:
PHP Version: OS:

 

 [2005-03-23 21:11 UTC] vblavet@php.net
Description:
------------
When multiplying two float, the truncated result is not correct.

Reproduce code:
---------------
<?php
$v1 = (float)0x5a05df1b;
$v2 = (float)0x2d02ef8d;

echo 'v1='.sprintf("0x%X (%d)", $v1, $v1).'<br>';
echo 'v2='.sprintf("0x%X (%d)", $v2, $v2).'<br>';
(float)$v3=(float)$v1*(float)$v2;
echo 'v1*v2='.sprintf("0x%X (%d)", (float)$v3, (float)$v3).'<br>';

// Actual result is :
//   v1=0x5A05DF1B (1510334235)
//   v2=0x2D02EF8D (755167117)
//   v1*v2=0x8DBC1700 (-1917053184)
//
// Result should be : 0xFD410808DBC16DF
// Truncated should be : 0x8DBC16DF

?>

Expected result:
----------------
Result should be : 0xFD410808DBC16DF, because variable is a float, result should be truncated like that :
0x8DBC16DF

Actual result:
--------------
v1*v2=0x8DBC1700 (-1917053184)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-03-23 21:42 UTC] derick@php.net
Why are you doing things like this?
 [2005-03-24 08:11 UTC] vblavet@php.net
I'm maintaining a PHP library that generate or extract zip files in PHP (PclZip on www.phpconcept.net).
Today this library does not support zip encryption. To support zip encryption I need to do this kind of complex math ... The code for encryption is quite ready, but failed because of this float truncate.
I wrote the bug report, because if it's a bug, and that it can be solved, I will have the ability to propose this encryption feature. If it can not be solved, then I will not try any more to do encryption with PclZip.

Thanks for your support !
Vincent
 [2005-03-24 18:08 UTC] sniper@php.net
See ext/bcmath and ext/gmp

 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Mar 12 04:01:30 2025 UTC