pow

C++ pow unusual type conversion

吃可爱长大的小学妹 提交于 2019-12-18 09:49:48
问题 When I directly output std::pow(10,2), I get 100 while doing (long)(pow(10,2)) gives 99. Can someone explained this please ? cout<<pow(10,2)<<endl; cout<<(long)(pow(10,2))<<endl; The code is basically this in the main function. The compiler is mingw32-g++.exe -std=c++11 using CodeBlocks Windows 8.1 if that helps 回答1: Floating point numbers are approximations. Occasionally you get a number that can be exactly represented, but don't count on it. 100 should be representable, but in this case it

GCC C++ pow accuracy

被刻印的时光 ゝ 提交于 2019-12-18 05:44:19
问题 So i was in a computing contest and i noticed a weird bug. pow(26,2) would always return 675, and sometimes 674? even though correct answer is 676. These sort of errors also occur with pow(26,3), pow(26,4) etc After some debugging after the contest i believe the answer has to do with the fact int rounds down. Interestingly this kind of error has never occured to me before. The computer i had was running mingw on windows 8. GCC version was fairly new, like 2-3 months old i believe. But what i

unusual output from pow

拜拜、爱过 提交于 2019-12-17 21:31:54
问题 The following C code int main(){ int n=10; int t1=pow(10,2); int t2=pow(n,2); int t3=2*pow(n,2); printf("%d\n",t1); printf("%d\n",t2); printf("%d\n",t3); return (0); } gives the following output 100 99 199 I am using a devcpp compiler. It does not make any sense, right? Any ideas? (That pow(10,2) is maybe something like 99.9999 does not explain the first output. Moreover, I got the same output even if I include math.h) 回答1: You are using a poor-quality math library. A good math library

negative pow in python

强颜欢笑 提交于 2019-12-17 19:51:20
问题 I have this problem >>> import math >>> math.pow(-1.07,1.3) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: math domain error any suggestion ? 回答1: (-1.07) 1.3 will not be a real number, thus the Math domain error. If you need a complex number, a b must be rewritten into e b ln a , e.g. >>> import cmath >>> cmath.exp(1.3 * cmath.log(-1.07)) (-0.6418264288034731-0.8833982926856789j) If you just want to return NaN, catch that exception. >>> import math >>> def

Does pow() work for int data type in C? [duplicate]

纵然是瞬间 提交于 2019-12-17 16:28:07
问题 This question already has answers here : Strange behaviour of the pow function (5 answers) Closed 4 years ago . I was simply writing a program to calculate the power of an integer. But the output was not as expected. It worked for all the integer numbers except for the power of 5. My code is: #include <stdio.h> #include <math.h> int main(void) { int a,b; printf("Enter the number."); scanf("\n%d",&a); b=pow(a,2); printf("\n%d",b); } The output is something like this: "Enter the number. 2 4

Math.Pow vs multiply operator (performance)

谁说胖子不能爱 提交于 2019-12-17 15:43:03
问题 Anyone knows if multiply operator is faster than using the Math.Pow method? Like: n * n * n vs Math.Pow ( n, 3 ) 回答1: Basically, you should benchmark to see. Educated Guesswork (unreliable): In case it's not optimized to the same thing by some compiler... It's very likely that x * x * x is faster than Math.Pow(x, 3) as Math.Pow has to deal with the problem in its general case, dealing with fractional powers and other issues, while x * x * x would just take a couple multiply instructions, so

Wrong result by Java Math.pow

房东的猫 提交于 2019-12-17 09:59:51
问题 If you try to run the following code public class Main { public static void main(String[] args) { long a = (long)Math.pow(13, 15); System.out.println(a + " " + a%13); } } You will get "51185893014090752 8" The correct value of 13^15 is 51185893014090757 , i.e. greater than the result returned by Math.pow by 5 . Any ideas of what may cause it? 回答1: You've exceeded the number of significant digits available (~15 to 16) in double-precision floating-point values. Once you do that, you can't

Wrong result by Java Math.pow

泄露秘密 提交于 2019-12-17 09:59:03
问题 If you try to run the following code public class Main { public static void main(String[] args) { long a = (long)Math.pow(13, 15); System.out.println(a + " " + a%13); } } You will get "51185893014090752 8" The correct value of 13^15 is 51185893014090757 , i.e. greater than the result returned by Math.pow by 5 . Any ideas of what may cause it? 回答1: You've exceeded the number of significant digits available (~15 to 16) in double-precision floating-point values. Once you do that, you can't

Why is Math.pow(0, 0) === 1?

旧巷老猫 提交于 2019-12-17 07:15:32
问题 We all know that 0 0 is indeterminate. But , javascript says that: Math.pow(0, 0) === 1 // true and C++ says the same thing: pow(0, 0) == 1 // true WHY? I know that: >Math.pow(0.001, 0.001) 0.9931160484209338 But why does Math.pow(0, 0) throw no errors? Or maybe a NaN would be better than 1 . 回答1: In C++ The result of pow(0, 0) the result is basically implementation defined behavior since mathematically we have a contradictory situation where N^0 should always be 1 but 0^N should always be 0

Exponentials in python x.**y vs math.pow(x, y)

穿精又带淫゛_ 提交于 2019-12-17 05:01:14
问题 Which one is more efficient using math.pow or the ** operator? When should I use one over the other? So far I know that x**y can return an int or a float if you use a decimal the function pow will return a float import math print math.pow(10, 2) print 10. ** 2 回答1: Using the power operator ** will be faster as it won’t have the overhead of a function call. You can see this if you disassemble the Python code: >>> dis.dis('7. ** i') 1 0 LOAD_CONST 0 (7.0) 3 LOAD_NAME 0 (i) 6 BINARY_POWER 7