cmath

Math interface vs cMath in C++

痞子三分冷 提交于 2019-11-27 22:24:01
The interface on my build system MacOS 10.6.3 for the POSIX math library is math.h, however on my target system the name of the interface file is cmath.h. At school we use cmath and I would like to be sure my project compiles when it is handed in, how is this achieved. The servers and workstations at school are x86 running Windows XP. The GCC is available on both platforms. In the C++ standard, the math library functions are defined in two headers: <cmath> contains them in the namespace std (e.g. std::sin ), while <math.h> contains them in the global namespace (so just sin ). There are further

How can I work around the fact that in C++, sin(M_PI) is not 0?

為{幸葍}努か 提交于 2019-11-27 17:38:40
问题 In C++, const double Pi = 3.14159265; cout << sin(Pi); // displays: 3.58979e-009 it SHOULD display the number zero I understand this is because Pi is being approximated, but is there any way I can have a value of Pi hardcoded into my program that will return 0 for sin(Pi)? (a different constant maybe?) In case you're wondering what I'm trying to do: I'm converting polar to rectangular, and while there are some printf() tricks I can do to print it as "0.00", it still doesn't consistently

When do I use fabs and when is it sufficient to use std::abs?

核能气质少年 提交于 2019-11-27 12:31:55
I assume that abs and fabs are behaving different when using math.h . But when I use just cmath and std::abs , do I have to use std::fabs or fabs ? Or isn't this defined? In C++, it's always sufficient to use std::abs ; it's overloaded for all the numerical types. In C, abs only works on integers, and you need fabs for floating point values. These are available in C++ (along with all of the C library), but there's no need to use them. It's still okay to use fabs for double and float arguments. I prefer this because it ensures that if I accidentally strip the std:: off the abs , that the

Why is pow(int, int) so slow?

别说谁变了你拦得住时间么 提交于 2019-11-27 11:04:31
问题 I've been working on a few project Euler exercises to improve my knowledge of C++. I've written the following function: int a = 0,b = 0,c = 0; for (a = 1; a <= SUMTOTAL; a++) { for (b = a+1; b <= SUMTOTAL-a; b++) { c = SUMTOTAL-(a+b); if (c == sqrt(pow(a,2)+pow(b,2)) && b < c) { std::cout << "a: " << a << " b: " << b << " c: "<< c << std::endl; std::cout << a * b * c << std::endl; } } } This computes in 17 milliseconds. However, if I change the line if (c == sqrt(pow(a,2)+pow(b,2)) && b < c)

Definitions of sqrt, sin, cos, pow etc. in cmath

杀马特。学长 韩版系。学妹 提交于 2019-11-27 10:09:31
问题 Are there any definitions of functions like sqrt() , sin() , cos() , tan() , log() , exp() (these from math.h/cmath) available ? I just wanted to know how do they work. 回答1: This is an interesting question, but reading sources of efficient libraries won't get you very far unless you happen to know the method used. Here are some pointers to help you understand the classical methods. My information is by no means accurate. The following methods are only the classical ones, particular

std::isfinite on MSVC

只愿长相守 提交于 2019-11-27 07:39:24
问题 The C++11 and C11 standard define the std::isfinite function. Visual Studio 2012 doesn't seem to provide it as part of the cmath or math.h , but has amp_math.h which seems to provide this function. Is the isfinite interchangeable with std::isfinite ? The documentation doesn't talk about the behavior when called with NAN and I don't have a VS compiler to test this. 回答1: As Marius has already pointed out, the isfinite from amp_math.h is to be used in C++ AMP, which is an MS extension for

Why are some functions in <cmath> not in the std namespace?

本秂侑毒 提交于 2019-11-27 04:22:36
I am developing a project which works with multiple arithmetic types. So I made a header, where the minimal requirements for a user defined arithmetic type are defined: user_defined_arithmetic.h : typedef double ArithmeticF; // The user chooses what type he // wants to use to represent a real number namespace arithmetic // and defines the functions related to that type { const ArithmeticF sin(const ArithmeticF& x); const ArithmeticF cos(const ArithmeticF& x); const ArithmeticF tan(const ArithmeticF& x); ... } What is troubling me is that when I use code like this: #include "user_defined

What is 1LL or 2LL in C and C++?

青春壹個敷衍的年華 提交于 2019-11-27 00:14:35
问题 I was looking at some of the solutions in Google Code Jam and some people used this things that I had never seen before. For example, 2LL*r+1LL What does 2LL and 1LL mean? Their includes look like this: #include <math.h> #include <algorithm> #define _USE_MATH_DEFINES or #include <cmath> 回答1: The LL makes the integer literal of type long long . So 2LL , is a 2 of type long long . Without the LL , the literal would only be of type int . This matters when you're doing stuff like this: 1 << 40

Is std::abs(0u) ill-formed?

邮差的信 提交于 2019-11-26 23:14:01
Given the following program: #include <cmath> int main() { std::abs(0u) ; } gcc and clang disagree on whether this is ill-formed. Using gcc with libstdc++ the code builds without error or warning ( see it live ), while using clang with libc++ it generates the following error ( see it live ): error: call to 'abs' is ambiguous std::abs(0u) ; ^~~~~~~~ Which result is correct? Should abs(0u) be ambiguous or not? MSalters points out an interesting related question: Template version of std::abs . Looks like libstdc++ is correct, this is not ill-formed, although we will see there are some doubts

Math interface vs cMath in C++

 ̄綄美尐妖づ 提交于 2019-11-26 20:58:33
问题 The interface on my build system MacOS 10.6.3 for the POSIX math library is math.h, however on my target system the name of the interface file is cmath.h. At school we use cmath and I would like to be sure my project compiles when it is handed in, how is this achieved. The servers and workstations at school are x86 running Windows XP. The GCC is available on both platforms. 回答1: In the C++ standard, the math library functions are defined in two headers: <cmath> contains them in the namespace