简体   繁体   English

确保股息大于除数的功能?

[英]Function to make sure dividend is greater than divisor?

Hey just curious is there a function that you can use to divide in "the right order", so that the dividend is always the larger number? 嘿,只是好奇吗,有一个函数可以用来按“正确的顺序”进行除法,以便使股息始终是更大的数字? Currently I do this 目前,我正在这样做

x=5
y=10
z=0

if x>y{
z = x/y
}else{
z = y/x
}

I was just wondering if there was a function I could use to do this in one line, like 我只是想知道是否有一个函数可以一行使用,例如

MakeSureDividendBigger(x/y) MakeSureDividendBigger(x / y)

type thing. 输入东西。

Thanks 谢谢

Javascript: Javascript:

var z = Math.max(x, y) / Math.min(x, y);

Or a function: 或功能:

function divideNice(x, y) {
    return Math.max(x, y) / Math.min(x, y);
}

var z = divideNice(x, y);

PHP: PHP:

$z = max($x, $y) / min($x, $y)

I doubt there is a special function for this, but possible one line solution is to use ternary conditional operator : 我怀疑对此有一个特殊的功能,但可能的一种解决方案是使用三元条件运算符

$z = $x > $y ? $x / $y : $y / $x;    // PHP

var z = x > y ? x / y : y / x;       // JavaScript

You can use Math.max and Math.min . 您可以使用Math.maxMath.min

Math.max(x,y) / Math.min(x,y)

Or in Mozilla (Firefox) you can easily do a swap. 或者在Mozilla(Firefox)中,您可以轻松进行交换。

if(x < y)
   [x,y] = [y,x]

x/y

This is called destructuring assignment . 这称为解构分配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM