百科狗-知识改变命运!
--

fmod() - 返回除法的浮点数余数 - php 数学函数

是丫丫呀1年前 (2023-11-21)阅读数 20#技术干货
文章标签余数

fmod()

(PHP 4 >= 4.2.0, PHP 5, PHP 7)

返回除法的浮点数余数

说明

fmod(float $x,float $y):float

返回被除数($x)除以除数($y)所得的浮点数余数。余数(r)的定义是:x = i * y + r,其中i是整数。如果$y是非零值,则r和$x的符号相同并且其数量值小于$y。

参数

$x

fmod() - 返回除法的浮点数余数 - php 数学函数

被除数

$y

除数

返回值

$x/$y的浮点数余数。

范例

Example #1fmod()的使用

WARNING: Due to how floating point numbers work, fmod() and any simple alternatives are problematic when there is either a massive orders of magnitude different between the input $x and $y, or the input and output values. If you need to work with large numbers or arbitrary precision, it is best to work with something like BC Math or GMP.
When working around fmod()'s problems, remember that floor() always goes towards -INF, not 0. This causes a commonly proposed fmod() alternative to only work with positive numbers:

Given these simplistic input values:
fmod_positive_only(-5, 3) = 1 (wrong)
-5 % 3 = -2 (correct)
Correctly removing the decimal part of the quotient can be achieved with either casting to an int (always goes towards zero) or dynamically choosing ceil() or floor(). Dynamically choosing floor or ceil in an attempt to keep precision is overkill. If your $x and $y values are so different that it suffers from an overflow problem when casting, it was probably going to have precision problems anyway (see warnings below).

This is the "best" alternative for fmod() when given "normal" numbers.

WARNING: Even when you get a non-zero response, know your input numbers and when fmod() can go wrong. For large values or depending on your input variable types, float still may not contain enough precision to get back the correct answer. Here are a few problems with fmod() and their alternatives.
PHP_INT_MAX = 9223372036854775807
fmod(PHP_INT_MAX, 2) = 0 (wrong)
fmod_alt(PHP_INT_MAX, 2) = 0 (wrong)
PHP_INT_MAX % 2 = 1 (correct)
fmod(PHP_INT_MAX, PHP_INT_MAX - 1) = 0 (wrong)
fmod_alt(PHP_INT_MAX, PHP_INT_MAX - 1) = 1 (correct)
fmod_alt(PHP_INT_MAX, PHP_INT_MAX - 1.0) = 0 (wrong)
PHP_INT_MAX % (PHP_INT_MAX - 1) = 1 (correct)
PHP_INT_MAX % (PHP_INT_MAX - 1.0) = 9223372036854775807 (wrong)
fmod(PHP_INT_MAX, 131) = 98 (wrong)
fmod_alt(PHP_INT_MAX, 131) = 359 (wrong)
fmod_positive_only(PHP_INT_MAX, 131) = 0 (wrong)
PHP_INT_MAX % 131 = 97 (correct)
I don't believe that is correct.
Try this out using your patch:

As of right now, this will read:
1 month, 22 days, 24 hours, 49 minutes, 15 seconds
Which is completely incorrect. Seeing as how it is the 9th of December.
The real real flaw here is how the 'year' and 'month' periods are calculated. As most months vary in length...
Thank you very much SnakeEater251 for pointing this out.
The quickest way to get slightly more accurate results, is to use averages based on one "true" year, which is 365.25 days.
Change the year and month to:
   'year'    => 31557600, // one 'true year' (365.25 days)
   'month'  => 2629800, // one 'true year' divided by 12 :-)
I will work on developing a true fix, for pin-point accuracy. ;-)
 - Cory Christison
This class ran through several unit tests and fixes all failures found in bugs.php.net 
Please note that this:

gives this:
fmod:     float(0.2)
custom_modulo: float(0)
Fmod does not deliver the desired result, therefore I made my own.
NAN (.net Equivalent = Double.NaN) means "Not-a-Number".
Some ways to get NaN are modulo 0, and square root of 0.
fmod() does not mirror a calculator's mod function. For example, fmod(.25, .05) will return .05 instead of 0 due to floor(). Using the aforementioned example, you may get 0 by replacing floor() with round() in a custom fmod(). 
Integer Modulo
If you want the remainder of the division of two Integers rather than Floats, use "%"; eg:

Will output "1".
If you need to reduce an integer to zero if zero and 1 if not, you can use
$sign = (integer)(boolean)$integer;
instead of
$sign = $integer > 0 ? 1 : 0;
it is faster from 100 operations on (at least on my machine).
Note that fmod is NOT equivalent to this basic function:

because fmod() will return a value with the same sign as $a. In other words the floor() function is not correct as it rounds towards -INF instead of towards zero.
To emulate fmod($a, $b) the correct way is:

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)