php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73442 Float fields always have precision 0
Submitted: 2016-11-02 12:57 UTC Modified: 2016-11-02 14:57 UTC
From: cmb@php.net Assigned: cmb (profile)
Status: Closed Package: dbase (PECL)
PHP Version: 5.6Git-2016-11-02 (Git) OS: *
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: cmb@php.net
New email:
PHP Version: OS:

 

 [2016-11-02 12:57 UTC] cmb@php.net
Description:
------------
When a DBF is created with dbase_create() all float fields ('F') have
their precision set to 0, so are in fact (inexact) integers[1][2].
Contrary to numeric fields ('N') there is no way to specify the field
length and precision.

Contrary to what one might assume, float fields are not supposed to
store real floating point numbers, but rather they are identical to
numeric fields with regard to the storage format. The only difference is
that as of dBASE IV (where float fields have been introduced) floats are
internally converted to IEEE floats (probably doubles) and numerics are
internally handled as BCD to offer exact calculations[3].

[1] <http://www.promotic.eu/en/pmdoc/Subsystems/Db/dBase/DataTypes.htm>
[2] <http://www.okstate.edu/sas/v7/sashtml/books/acespc/z0214453.htm>
[3] <http://www.oocities.org/geoff_wass/dBASE/GaryWhite/dBASE/FAQ/qfloat.htm>


Test script:
---------------
<?php

$filename = __DIR__ . DIRECTORY_SEPARATOR . 'test.dbf';

$db = dbase_create($filename, [['FLOAT', 'F', 15, 5]]);
dbase_close($db);

$stream = fopen($filename, 'r');
fseek($stream, 48);
$bytes = fread($stream, 2);
var_dump(unpack('Clength/Cprecision', $bytes));


Expected result:
----------------
array(2) {
  ["length"]=>
  int(15)
  ["precision"]=>
  int(5)
}

Actual result:
--------------
array(2) {
  ["length"]=>
  int(20)
  ["precision"]=>
  int(0)
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-11-02 12:57 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2016-11-02 14:56 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=340843
Log: Fix #73442: Float fields always have precision 0

For compatibility with other dBASE tools, float fields have to be
treated like numeric fields, i.e. length and precision have to be
specified when a new DBF is created.
 [2016-11-02 14:57 UTC] cmb@php.net
-Status: Assigned +Status: Closed
 [2016-11-03 14:28 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=340851
Log: Fix #73442: Float fields always have precision 0

Ammendment for r340843: dbase_get_header_info() also has to report the
float field precision.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 10:01:28 2024 UTC