如何编写一个函数来实现整数除法而不在PHP中使用除法运算符 [英] how to write a function to implement an integer division algorithm without using the division operator in php

查看:72
本文介绍了如何编写一个函数来实现整数除法而不在PHP中使用除法运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写不带整数除法的函数 使用除法运算符.浮点值和余数可能 被丢弃.错误条件可能会被忽略.

How to write a function to implement an integer division algorithm without using the division operator. Floating point values and remainders may be discarded. Error conditions may be ignored.

例如:

f(10, 3) is 3

f(10, 5) is 2

f(55, 5) is 11

推荐答案

我的实现,但未考虑操作数的符号

My implementation, but it does not take into account signs of operands

function f($value, $div)
{
        $result = 0;
        while ($value >= $div) {
                $result++;
                $value -= $div;
        }

        return $result;
}

var_dump(f(10,3));

这篇关于如何编写一个函数来实现整数除法而不在PHP中使用除法运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆