- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 C 初学者,在 JavaScript 之后开始学习,无法适应这些类型。
好吧,math.h 的 sqrt 函数应该使用 double ,据我所知,在 C 中你不能将错误的类型作为参数传递。
但是当我去的时候:
int b = sqrt(1234); //it works
也是这样
float b = sqrt(1234); // it works
int b = sqrt(1234.22) // it works
为什么这些都有效?这里的流程是什么样的?
我的猜测是:无论我传递什么,sqrt的参数都会自动转换为double,如果我分配给的变量是 int 类型,则结果 double 会转换为 int。
那么两个问题?
1) 如果我传递了错误的类型,为什么其他函数会出现错误,而 sqrt 却不会?2) 如果我们可以像这样将 int 转换为 float
float b = 123.44
int a = b;
为什么我们需要这个?
float b = 123.44
int a = (int) b;
最佳答案
Why are all these [two initializations] work?
第一次初始化float b = sqrt(1234)
之所以有效,是因为该语言“向上转换”了整数文字 1234
至double
在调用sqrt
之前,然后将结果转换为float
.
第二次初始化int b = sqrt(1234.22)
出于同样的原因,除了这次编译器不必向上转换 1234.22
调用之前的文字,因为它已经是 double
类型.
这在 C99 标准中进行了讨论:
6.3.1.4.1: When a finite value of real floating type is converted to an integer type other than
_Bool
, the fractional part is discarded.6.7.8.11: The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion) (emphasis added).
-
why do we need this [cast
int a = (int) b;
]?
您可以插入强制转换以提高可读性,但标准并不要求它 ( demo )。
关于C:数据类型。 sqrt 函数与 int 一起使用为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27105695/
我在玩(美丽的)多项式 x^4 - 10x^2 + 1 . 看看会发生什么: In[46]:= f[x_] := x^4 - 10x^2 + 1 a = Sqrt[2];
对于整个参数范围 x,a >= 0,是否有一种优雅的数值稳定评估以下表达式的方法? f(x,a) = sqrt(x+a) - sqrt(x) 还有没有提供这种功能的任何编程语言或库?如果是,以什么名义
我正在制作自定义运算符 (≠,≈,Σ,√),平方根的实现很有趣。我写的 prefix func √ (item:Double) -> Double { return sqrt(item) }
这是我每秒调用多次的函数: static inline double calculate_scale(double n) { //n may be int or double return s
我对 C++ 很陌生,我有这段代码,代码如下所示: D = (sum_B / double(E))*std::sqrt(E) 有人可以将其解释为数学公式或易于理解的东西吗,我不确定这是什么 std::
这个问题在这里已经有了答案: Can a declaration affect the std namespace? (2 个答案) Why doesn't adding sqrt() cause
考虑以下代码: #include #include const int COUNT = 100000000; int main() { double sum = 0; for (i
x**(1/2)、math.sqrt() 和 cmath.sqrt() 有什么区别? 为什么 cmath.sqrt() 单独得到二次项的复根?我应该专门将它用于我的平方根吗?他们在后台做了什么不同的事
我创建了一个小程序,如下: #include #include #include int main(int argc, char *argv[]) { int i;
我得到了如下表达式(Sqrt[XXX] 的数量未知) Sqrt[A+B] + Sqrt[Min[A,B]] * Min[Sqrt[C],D] 我想把所有的 Sqrt[XXX] 变成 Sqrt(XXX)
这次重复: T(n) = sqrt(n) * T(sqrt(n)) + n 它似乎无法用 Master 定理求解。它似乎也无法用 Akra-Bazzi 解决。即使我设置 n = 2^k 以便 T(2^
在数学中,恒等式 (1 + sqrt(2))^2 = 3 + 2*sqrt(2) 成立。但在浮点(IEEE 754,使用单精度,即 32 位)计算中,情况并非如此,因为 sqrt(2) 没有精确的二进
我创建了一个小程序,如下: #include #include #include int main(int argc, char *argv[]) { int i;
这给了我 0: int B=-4; double A = Math.Sqrt(1/B); 但是这个 NaN double A = Math.Sqrt(-4); 第一次计算怎么可能不失败或者至少不返回
template T sqrt (T); template complex sqrt(complex); double sqrt(double); void f(complex z) { sq
这里是 Python 的新手。我试图了解此函数如何检查素数: from itertools import count, islice from math import sqrt def is_prim
上述复杂性的大 O 表示法是什么? 是不是O(n) 最佳答案 是的。关键是日志里面的平方根没有区别: O(sqrt(n) log(sqrt(n))) = O(sqrt(n) 1/2 log(n)) =
如果 g(n) = sqrt(n)sqrt(n),g(n) = O(2n) 的复杂度是多少? 感谢任何帮助。 最佳答案 比较两个指数函数时,一个有用的技巧是让它们具有相同的基数: √n√n = (2l
我正在查看我的代码,希望提高它的性能,然后我看到了这个: int sqrt = (int) Math.floor(Math.sqrt(n)); 哦,好的,我真的不需要调用 Math.floor,因为转
我试图找到满足子句 (x - y * √ 2016)/(y + √ 2016) = 2016 的数字。数字 x 和 y 可以是有理数。 这是我已经尝试过的: #include #include #
我是一名优秀的程序员,十分优秀!