Home > OS >  Why doesn't numeric require/allow std::, but cmath does it?
Why doesn't numeric require/allow std::, but cmath does it?

Time:12-31

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 experiment is removed all the std:: and it works fine and compiled fine. I decided to include cmath as well and commented out the #include <numeric> and now all the math functions again have squiggles. I reinserted the std:: and squiggles disappeared. I did some google research and it seems to me 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,its a YouTube tutorial thus it is 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. heres 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 #include <cmath> is an alias for the Standard 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 "cmath" or "math.h".

None of the functions in these headers are in the C namespace "std"; hence they don't need to be qualified with "std::xxx".

  • Related