Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:50:15+00:00 2026-06-03T06:50:15+00:00

If I have some mathematical equations which rely on inputs which can be zero

  • 0

If I have some mathematical equations which rely on inputs which can be zero or non-zero (template argument, known at compile time), will the optimiser evaluate the equations and optimise out expressions it knows will evaluate to 0 or 1.

For example:

double x = y * Eval<type>::value;

if Eval<type>::value is 0, x will always be 0.

double x = exp(y * Eval<type>::value);

if Eval<type>::value is 0, x will always be 1.

Can the optimiser figure this out and replace x with 0 or 1 elsewhere in the code, or will these calculations be carried out at runtime?

I am using gcc 4.7 with -O3

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:50:16+00:00Added an answer on June 3, 2026 at 6:50 am

    EDIT: I was wrong, the compiler works as expected when using a floating point number.

    Well gcc 4.6.3 in -O3 certainly does seem to do this, as long as the expression is integer related.

    Example code:

    #include <cstdio>
    inline int x(double y)
    {
       if (y == 0)
          printf("Hello bob3\n");
       else
          printf("Why do I bother\n");
    
    };
    
    const int c = 0;
    
    int main()
    {
       int f;
       scanf("%d",&f);
    
       x(f * c);
    }
    

    Resulting assembly

        .file   "foo.cpp"
        .section    .rodata.str1.1,"aMS",@progbits,1
    .LC0:
        .string "%d"
    .LC1:
        .string "Hello bob3"
        .section    .text.startup,"ax",@progbits
        .p2align 4,,15
        .globl  main
        .type   main, @function
    main:
    .LFB13:
        .cfi_startproc
        subq    $24, %rsp
        .cfi_def_cfa_offset 32
        movl    $.LC0, %edi
        xorl    %eax, %eax
        leaq    12(%rsp), %rsi
        call    scanf
        movl    $.LC1, %edi
        call    puts
        xorl    %eax, %eax
        addq    $24, %rsp
        .cfi_def_cfa_offset 8
        ret
        .cfi_endproc
    .LFE13:
        .size   main, .-main
        .ident  "GCC: (Debian 4.6.3-1) 4.6.3"
        .section    .note.GNU-stack,"",@progbits
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Java code which performs bitwise operations on a BitSet. I have
I have some XML which contains records and sub records, like this: <data> <record
I have some code which populates a hashtable with a question as the key
I am developing an android app. for mathematical calculations. There I have some buttons
I'm trying to write a small Python module which contain some mathematical functions. For
i have an array of some values that i use in a mathematical formula,
I have to work with some (semi-)automatical verification software (CBMC (link) ) which is
I have some XML data representing a mathematical expression tree and want to convert
I have some mathematical formulas of the type p = (l + m +
I have seen many recursive functions(mostly used in computing some mathematical operations e.g. factorial,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.