| 
     
      
      
      From: <gem...@li...> - 2012-11-22 15:59:58
      
     
   | 
Revision: 1037
          http://gemstracker.svn.sourceforge.net/gemstracker/?rev=1037&view=rev
Author:   matijsdejong
Date:     2012-11-22 15:59:49 +0000 (Thu, 22 Nov 2012)
Log Message:
-----------
Added decimal functions provided by Roel
Added Paths:
-----------
    trunk/library/classes/MUtil/Dec.php
    trunk/test/classes/MUtil/DecTest.php
Added: trunk/library/classes/MUtil/Dec.php
===================================================================
--- trunk/library/classes/MUtil/Dec.php	                        (rev 0)
+++ trunk/library/classes/MUtil/Dec.php	2012-11-22 15:59:49 UTC (rev 1037)
@@ -0,0 +1,87 @@
+<?php
+
+/**
+ * Copyright (c) 2011, Erasmus MC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *    * Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *    * Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *    * Neither the name of Erasmus MC nor the
+ *      names of its contributors may be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * @package    MUtil
+ * @subpackage Math
+ * @author     Matijs de Jong <mj...@ma...>
+ * @copyright  Copyright (c) 2011 Erasmus MC
+ * @license    New BSD License
+ * @version    $Id$
+ */
+
+/**
+ * Decimal calculation utilities
+ *
+ * RF:  winbill at hotmail dot com 16-Dec-2010 09:31
+ * There are issues around float rounding/flooring,
+ * use of intermediate typecasting to string (strval) avoids problems
+ *
+ * jolyon at mways dot co dot uk 10-Aug-2004 11:41
+ * The thing to remember here is that the way a float stores a value makes it
+ * very easy for these kind of things to happen. When 79.99 is multiplied
+ * by 100, the actual value stored in the float is probably something like
+ * 7998.9999999999999999999999999999999999, PHP would print out 7999 when the
+ * value is displayed but floor would therefore round this down to 7998.
+ *
+ *
+ * @package    MUtil
+ * @subpackage Math
+ * @copyright  Copyright (c) 2011 Erasmus MC
+ * @license    New BSD License
+ * @since      Class available since version 1.5
+ */
+class MUtil_Dec
+{
+    /**
+     * Get the floor using the specified precision
+     *
+     * @param float $number
+     * @param int $precision
+     * @return float
+     */
+    public static function floor($number, $precision)
+    {
+        $coefficient = pow(10,$precision);
+        return floor(strval($number*$coefficient))/$coefficient;
+    }
+
+    /**
+     * Get the ceiling using the specified precision
+     *
+     * @param float $number
+     * @param int $precision
+     * @return float
+     */
+    public static function ceil($number, $precision)
+    {
+        $coefficient = pow(10,$precision);
+        return ceil(strval($number*$coefficient))/$coefficient;
+    }
+
+}
Added: trunk/test/classes/MUtil/DecTest.php
===================================================================
--- trunk/test/classes/MUtil/DecTest.php	                        (rev 0)
+++ trunk/test/classes/MUtil/DecTest.php	2012-11-22 15:59:49 UTC (rev 1037)
@@ -0,0 +1,113 @@
+<?php
+
+
+/**
+ * Copyright (c) 2011, Erasmus MC
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *    * Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *    * Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *    * Neither the name of Erasmus MC nor the
+ *      names of its contributors may be used to endorse or promote products
+ *      derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ *
+ * @package    MUtil
+ * @subpackage Dec
+ * @author     Matijs de Jong <mj...@ma...>
+ * @copyright  Copyright (c) 2011 Erasmus MC
+ * @license    New BSD License
+ * @version    $Id$
+ */
+
+require_once 'PHPUnit/Framework/TestCase.php';
+
+/**
+ * Unit test for class MUtil_String
+ *
+ * @package    MUtil
+ * @subpackage Dec
+ * @copyright  Copyright (c) 2011 Erasmus MC
+ * @license    New BSD License
+ * @since      Class available since version 1.5.7
+ */
+class MUtil_DecTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Dataprovider
+     *
+     * @return array
+     */
+    public static function forCeiling()
+    {
+        return array(
+            array(10.49825, 1, 10.5),
+            array(10.99825, 1, 11.0),
+            array(10.09825, 1, 10.1),
+            array(10.09825, 2, 10.10),
+            array(10.09825, 3, 10.099),
+            array(79.99*100, 0, 7999),
+            array(79.99*100, -1, 8000),
+            array((10.02-10)*100, 1, 2.0),
+            );
+    }
+
+    /**
+     * Dataprovider
+     *
+     * @return array
+     */
+    public static function forFloor()
+    {
+        return array(
+            array(10.49825, 1, 10.4),
+            array(10.99825, 1, 10.9),
+            array(10.09825, 1, 10.0),
+            array(10.09825, 2, 10.09),
+            array(10.09825, 3, 10.098),
+            array(79.99*100, 0, 7999),
+            array(79.99*100, -1, 7990),
+            array((10.02-10)*100, 1, 2.0),
+            );
+    }
+
+    /**
+     *
+     * @dataProvider forCeiling
+     * @param float $float
+     * @param int $precision
+     * @param float $output
+     */
+    public function testCeil($float, $precision, $output)
+    {
+        $this->assertEquals($output, MUtil_Dec::ceil($float, $precision));
+    }
+
+    /**
+     *
+     * @dataProvider forFloor
+     * @param float $float
+     * @param int $precision
+     * @param float $output
+     */
+    public function testFloor($float, $precision, $output)
+    {
+        $this->assertEquals($output, MUtil_Dec::floor($float, $precision));
+    }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
 |