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

  • Home
  • SEARCH
  • 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 4049124
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:54:19+00:00 2026-05-20T13:54:19+00:00

I have a class for fixed-point arithmetic, of which this is the salient portion:

  • 0

I have a class for fixed-point arithmetic, of which this is the salient portion:

template <typename I, I S>
struct fixed
{
    I value;

    fixed(I i) : value(i * S) {}

    template <typename J, J T> fixed(const fixed<J, T> &fx)
    {
        if (S % T == 0)
            value = fx.value * (S / T);
        else if (T % S == 0)
            value = fx.value / (T / S);
        else
            value = S * fx.value / T;
    }

    static_assert(S >= 1, "Fixed-point scales must be at least 1.");
};

On GCC 4.4.5, the following line of code:

fixed<int, 8> f = fixed<int, 2>(1);

Generates an error:

fixed.hpp: In constructor ‘fixed<I, S>::fixed(const fixed<J, T>&) [with J = int, J T =     2, I = int, I S = 8]’:
fixed.hpp:81: error: division by zero

While there is a division by constant zero in the code – one of T/S or S/T must be zero for unequal scales – if S%T == 0 (and S is not 0), then S/T is not zero. GCC seems to be doing just enough optimization to figure out that one of my branches is guaranteed to divide by zero, but not enough optimization to figure out that branch guaranteed to not run.

I can throw #pragma GCC diagnostic ignored "-Wdiv-by-zero" in the file, but that risks masking real warnings.

What’s the appropriate way to handle this situation? (Or is my analysis totally wrong and I do have a real runtime division by zero?)

  • 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-05-20T13:54:20+00:00Added an answer on May 20, 2026 at 1:54 pm

    something like?

    template<int,int>
    struct helper {
        static int apply(...) { return S * fx.value / T; }
    };
    
    template<int n>
    struct helper<0,n> { // need 0,0 as well to avoid ambiguity
        static int apply(...) { return fx.value * (S / T); }
    };
    
    template<int m>
    struct helper<m,0> {
        static int apply(...) { return fx.value / (T / S); }
    };
    
    helper<(S % T == 0), (T % S == 0)>::apply(...);
    

    or using mpl::bool_ you could “specialize” functions through parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that encapsulates some arithmetic, let's say fixed point calculations. I
I have a Fixed Point type by subclassing Type (refer to this post ).
I've got a fixed point class (10.22) and I have a need of a
I have class A, which exposes an event. an object of class B subscribed
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I am trying to implement a fixed-point class in C++, but I face problems
I need to make fixed point number class inherit from System.Type. class FixedPoint :
I have a class which calculates a date and I'm wondering how best to
I have implemented a C++ class which behaves very similarly to the standard int
I have class A: public B { ...} vector<A*> v; I want to do

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.