看下面的例子,它看起来Math.floor(x)相当于x | 0, for x >= 0。这是真的吗?如果是,为什么?(或者是如何x | 0计算的?)
x = -2.9; console.log(Math.floor(x) + ", " + (x | 0)); // -3, -2
x = -2.3; console.log(Math.floor(x) + ", " + (x | 0)); // -3, -2
x = -2; console.log(Math.floor(x) + ", " + (x | 0)); // -2, -2
x = -0.5; console.log(Math.floor(x) + ", " + (x | 0)); // -1, 0
x = 0; console.log(Math.floor(x) + ", " + …Run Code Online (Sandbox Code Playgroud) 我目前正在做一个随机数学测验,它可以生成随机加法、减法和乘法。这3个很简单。我想知道如何使用 2 个 int 变量 num1 和 num2 制作一个始终等于整数的随机生成的除法问题,但我就是无法弄清楚任何帮助将不胜感激!
我知道浮点不准确,并且想知道获得的最佳方法, 0.08354而不是(0.08353999999999999)当我在 Python 中执行以下操作时:
d = 8.354/100
print (d)
Run Code Online (Sandbox Code Playgroud) 在 GHCI 中,我得到以下结果:
div (-7) ( 2) = -4
div ( 7) (-2) = -4
div (-7) (-2) = 3
mod ( 7) ( 2) = 1
mod ( 7) (-2) = -1
mod (-7) ( 2) = 1
mod (-7) (-2) = -1
Run Code Online (Sandbox Code Playgroud)
并且div 4 (-3)是-2并且mod 4 (-3)是-2。
我无法理解如何获得这些结果。有什么法律或规则吗?什么是正确的计算步骤?
我试图检查一个数字是否可以被另一个整除,目前我使用这种方法:
int x = 70;
int y = 30;
if(x/y == Math.round(x/y)) {
...
}
Run Code Online (Sandbox Code Playgroud)
有没有更简单的方法?
我有以下代码:
int x = arr[arr.size() -1] - arr[0];
int n = arr.size();
int dx = x/n;
int dy = (arr[arr.size() -1] - arr[0])/arr.size();
std::cout << dx << " " << dy << std::endl;
Run Code Online (Sandbox Code Playgroud)
当我的arr = [15, 13, 12],我得到了dx = -1,但是dy = 1431655764( arr is vector<int>)
为什么dx和dy不同?谢谢!
当我用 numpy 对函数进行向量化时,我得到了一个奇怪的结果。
import numpy as np
def scalar_function(x, y):
""" A function that returns x*y if x<y and x/y otherwise
"""
if x < y :
out = x * y
else:
out = x/y
return out
def vector_function(x, y):
"""
Make it possible to accept vectors as input
"""
v_scalar_function = np.vectorize(scalar_function)
return v_scalar_function(x, y)
Run Code Online (Sandbox Code Playgroud)
我们确实有
scalar_function(4,3)
# 1.3333333333333333
Run Code Online (Sandbox Code Playgroud)
为什么矢量化版本会给出这种奇怪的输出?
vector_function(np.array([3,4]), np.array([4,3]))
[12 1]
Run Code Online (Sandbox Code Playgroud)
虽然对矢量化版本的调用工作正常:
vector_function(np.array([4,4]), np.array([4,3]))
[1. 1.33333333]
Run Code Online (Sandbox Code Playgroud)
阅读numpy.divide:
注释 Python 2.2 中添加了向下除法运算符 //,使 // …
这应该是一个递归函数,当 i 小于 'h' 时,它不断向 'h' 添加 1/i 的值,但我在函数声明中不断收到冲突类型错误,我做错了什么?它只是发生在浮动中。
#include <stdio.h>
int main()
{
int i=0, n = 5;
float h;
division(i, n, h);
return 0;
}
float division(int i, int n, float h)
{
if(i<n)
{
h += 1/i;
division(i, n, h);
}
else
{
printf("the sum is: %f",h);
return h;
}
}
Run Code Online (Sandbox Code Playgroud) c floating-point runtime-error division function-declaration
我想编写一个 Haskell 函数,它需要两个Integers 并输出一个Double.
签名应如下所示:
divide :: Integer -> Integer -> Double
Run Code Online (Sandbox Code Playgroud)
我想要的功能是:
divide x y = x / y
Run Code Online (Sandbox Code Playgroud)
我用这个函数得到的错误信息是:
Couldn't match expected type ‘Double’ with actual type ‘Integer’
Run Code Online (Sandbox Code Playgroud)
如何获得此函数的正确双重结果?
int在 C++ 中对两个值执行除法而不使用时,如何进行四舍五入std::ceil?
通常,我最终会执行以下操作:
double res = ceil(a / (double) b);
Run Code Online (Sandbox Code Playgroud)
有什么方法可以在不使用的情况下复制结果std::ceil?