constantfolding

Why 0.1 + 0.2 == 0.3 in D?

旧街凉风 提交于 2019-11-27 17:29:10
assert(0.1 + 0.2 != 0.3); // shall be true is my favorite check that a language uses native floating point arithmetic. C++ #include <cstdio> int main() { printf("%d\n", (0.1 + 0.2 != 0.3)); return 0; } Output: 1 http://ideone.com/ErBMd Python print(0.1 + 0.2 != 0.3) Output: True http://ideone.com/TuKsd Other examples Java: http://ideone.com/EPO6X C#: http://ideone.com/s14tV Why is this not true for D? As understand D uses native floating point numbers. Is this a bug? Do they use some specific number representation? Something else? Pretty confusing. D import std.stdio; void main() { writeln(0.1

log(10.0) can compile but log(0.0) cannot with undefined reference?

梦想与她 提交于 2019-11-27 08:48:31
For the following C source code: #include <math.h> int main(void) { double x; x = log(0.0); return 0; } When I compile with gcc -lm , I got: /tmp/ccxxANVH.o: In function `main': a.c:(.text+0xd): undefined reference to `log' collect2: error: ld returned 1 exit status But, if I replace log(0.0) with log(10.0) , then it can compile successfully. I don't quite understand this, since no matter they make mathematical sense or not, they should compile -- there is no syntax error. Could anyone explain this? Just in case, my gcc -v output: Configured with: ../src/configure -v --with-pkgversion='Ubuntu

is there any concept called “Constant Folding” in java?

大憨熊 提交于 2019-11-26 18:36:37
问题 is there any concept called "Constant Folding" in java? if yes what is it? 回答1: Constant folding is the process of simplifying constant expressions at compile time. Terms in constant expressions are typically simple literals, such as the integer 2, but can also be variables whose values are never modified, or variables explicitly marked as constant Yes, it's exists on Java: Compiler optimizations 回答2: Yes, there is. From this JavaWorld article ( which you could've googled yourself! ): static

log(10.0) can compile but log(0.0) cannot with undefined reference?

醉酒当歌 提交于 2019-11-26 14:18:33
问题 For the following C source code: #include <math.h> int main(void) { double x; x = log(0.0); return 0; } When I compile with gcc -lm , I got: /tmp/ccxxANVH.o: In function `main': a.c:(.text+0xd): undefined reference to `log' collect2: error: ld returned 1 exit status But, if I replace log(0.0) with log(10.0) , then it can compile successfully. I don't quite understand this, since no matter they make mathematical sense or not, they should compile -- there is no syntax error. Could anyone