Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

130
Views
Can the standard math functions handle infinity correctly?
int main() {
    double inf = INFINITY;
    double pi = acos(-1.0);
    printf("[1]: %f %f\n", atan(inf) / pi, atan(-inf) / pi);
    printf("[2]: %f %f\n", tan(inf) / pi, tan(-inf) / pi);
    return 0;
}

outputs

[1]: 0.500000 -0.500000
[2]: -nan -nan

Is such behaviour defined by the standard? Is [2] undefined behaviour? unspecified?

I want to be sure at least [1] is a guaranteed result.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Both are well defined behaviour. Quoting from http://en.cppreference.com

  • tan

    If the argument is ±0, it is returned unmodified.
    If the argument is ±∞, NaN is returned and FE_INVALID is raised.
    If the argument is NaN, NaN is returned.

  • atan

    If the argument is ±0, it is returned unmodified.
    If the argument is +∞, +π/2 is returned.
    If the argument is -∞, -π/2 is returned.
    If the argument is NaN, NaN is returned.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!