【问题标题】:Is there a fixed-point library for actionscript 3?是否有用于 actionscript 3 的定点库?
【发布时间】:2010-02-06 17:45:11
【问题描述】:

我想在 Flex 中编写一个计算器,但在网上找不到任何定点库。

对于计算器,我需要比 IEEE 754 能保证的更高的精度。例如:

trace(1.4 - .4); //should be 1 but it is 0.9999999999999999

有人可以推荐一个好的定点库吗?

提前谢谢你

【问题讨论】:

    标签: apache-flex actionscript-3 fixed-point


    【解决方案1】:

    它有效,但并不完美,所有学分都来自http://joshblog.net/2007/01/30/flash-floating-point-number-errors/的乔希

    /**
     * Corrects errors caused by floating point math.
     */
    public function correctFloatingPointError(number:Number, precision:int = 5):Number
    {
        //default returns (10000 * number) / 10000
        //should correct very small floating point errors
        var correction:Number = Math.pow(10, precision);
        return Math.round(correction * number) / correction;
    }
    /**
     * Tests if two numbers are <em>almost</em> equal.
     */
    public function fuzzyEquals(number1:Number, number2:Number, precision:int = 5):Boolean
    {
        var difference:Number = number1 - number2;
        var range:Number = Math.pow(10, -precision);
        //default check:
        //0.00001 <difference> -0.00001
        return difference <range && difference> -range;
    }
    /*
    Copyright (c) 2007 Josh Tynjala
    Permission is hereby granted, free of charge, to any person
    obtaining a copy of this software and associated documentation
    files (the "Software"), to deal in the Software without
    restriction, including without limitation the rights to use,
    copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the
    Software is furnished to do so, subject to the following
    conditions:
    The above copyright notice and this permission notice shall be
    included in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
    OTHER DEALINGS IN THE SOFTWARE.
    */
    

    再次感谢您:)

    【讨论】:

      【解决方案2】:

      我不知道,但与此同时你可以使用这个 sn-p 代码(不是我的):

          public static function correctFloatingPointError(number:Number, precision:int = 5):Number
          {
              //default returns (10000 * number) / 10000
              //should correct very small floating point errors
      
              var correction:Number = Math.pow(10, precision);
              return Math.round(correction * number) / correction;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多