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 928379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:59:58+00:00 2026-05-15T19:59:58+00:00

Is there a way in C++ to have it evaluate each floating point operation

  • 0

Is there a way in C++ to have it evaluate each floating point operation as a double even if the arguments are int? I have a program and numerous places I have code such as 1/2, 5/6. In these cases C++ casts the result to an int, that screws up the whole calculation. From the perspective of financial computations, are there other libraries besides ‘cmath’ that I could use that would contain more advanced functions.

  • 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-15T19:59:59+00:00Added an answer on May 15, 2026 at 7:59 pm

    In C (and thus C++) all builtin operators (ie POD) operato on objects of the same type. Thus if the parameters of a builtin operator have different types then the compiler will implicitly cast one (usually) of them (according to well defined rules) so that they are both the same. The result of the operator is also the same type as the input parameters.

    What you are seeing is integer division which returns an integer.

    The implicit casting rules:

    1) If either value is long double then cast the other to long doubele  (technically only C)
    2) If either value is      double then cast the other to      double
    3) If either value is      float  then cast the other to      float
    4) If either value is unsi long   then cast the other to unsi long
    5) If either value is      long   then cast the other to      long
    6) If either value is unsi int    then cast the other to unsi int
    7) Cast both values to int
    

    Note: All arithmatic operation are done on at least int. This means if both parameters are (u)short and/or char then they are both cast to int.

    From this we can we can see that to get the operation to happen on doubles then at least one of the parameters must be a double in the code. To do this just add a fractional part to the expression.

    int val = 1.0/2;
    
    // This is equivalent to:
    
    int val = static_cast<int>( 1.0 / static_cast<double>(2));
    // Are you not glad the compiler does the implicit cast for you :-)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to have setup/teardown code automagically run before/after each test? Something
is there any way to set Func<> type arguments dynamically, so i don't have
Is there a way to have a template specialization based on a range of
Is there a way to have ASP classic cdonts email, have an file attached
Is there a way to have hanging operators when indenting with tabs in Vim?
Is there a way to have rails print out a number with commas in
is there a way to have the cobertura test coverage graph be shown on
Is there any way to have a label wordwrap text as needed? I have
Is there a way to have an animated loading screen for my iPhone application
Is there any way to have to tabcontrol take the size of the largest

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.