Why, at least in my code, does <numeric>
NOT require or ALLOW std::
?
I'm following along with a YouTube SDL2 tutorial, and IntelliSense has squiggled all my trig functions. In the process of experimenting, I removed all the std::
and it worked and compiled fine. I decided to add #include <cmath>
as well and commented out #include <numeric>
, and now all the math functions again have squiggles. I reinserted the std::
and the squiggles disappeared.
I did some Google research and it seems to me that <numeric>
is part of the standard library and thus should require std::
.
#include "screen.h"
#include <numeric>
//#include <cmath>
void line(Screen& screen, float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
float length = float (sqrt(dx * dx dy * dy));
float angle = float (atan2(dy, dx));
for (float i = 0; i < length; i )
{
screen.pixel(
x1 float (cos(angle) * i),
y1 float (sin(angle) * i)
);
}
}
int main(int argv, char** args)
{
Screen screen;
for (int i = 0; i < 100; i )
{
screen.pixel(rand() % 640, rand() % 480);
}
line(screen, 0, 0, 300, 300);
while (true)
{
screen.show();
screen.input();
}
return 0;
}
OK, once again, the code is NOT mine, it's a YouTube tutorial, thus it's his code, and this code compiled using <numeric>
only for math functions such as sin()
, cos()
, atan2()
and sqrt()
. My one modification was to include <cmath>
.
Here's my reproducible example:
//#include "screen.h"
#include <numeric>
#include <iostream>
//#include <cmath>
void line( float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
float length = float (sqrt(dx * dx dy * dy));
float angle = float (atan2(dy, dx));
for (float i = 0; i < length; i )
{
float tempx1 = x1 float(cos(angle) * i);
float tempy1 = y1 float(sin(angle) * i);
std::cout << i << " " << x1 << " " << y1 << std::endl;
}
}
int main(int argv, char** args)
{
line(2, 2, 20, 20);
return 0;
}
CodePudding user response:
The C header <cmath>
declares exactly the same Standard C math functions also declared in the C header <math.h>
, with functions like pow()
, ceil()
, sqrt()
, tan()
, and constants like M_PI
.
The C header <numeric>
is completely different:
Components for performing numeric operations.
Includes support for complex number types, random number generation,
numeric (n-at-a-time) arrays, generalized numeric algorithms,
and mathematical special functions.
If you happen to have some header named numeric.h
on your workstation ... it's completely unrelated to either of the above headers. For example:
Directory of c:\Ruby30-x64\include\ruby-3.0.0\ruby\internal\intern
07/09/2021 07:38 PM 1,941 numeric.h
If you want to use the Standard C library math functions sqrt()
or cos()
(callable from either C or C ), then you need to #include
either <cmath>
or <math.h>
.
None of the functions in <math.h>
are in the C namespace std
, hence they don't need to be qualified with std::xxx
.