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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:10:56+00:00 2026-06-13T17:10:56+00:00

When I try to divide 1/60 or 1/(60*60) it gives 0. Even in debugger

  • 0

When I try to divide 1/60 or 1/(60*60) it gives 0. Even in debugger window. I am a bit confused what it could be, because 2/3 or 2.5/6 give results.

My code:

int main()
{   
    double k1 = 1/60;
    cout << k1 << endl;
    double k2 = 1/(60*60);
    cout << k2 << endl;

    return 0;
}
  • 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-13T17:10:58+00:00Added an answer on June 13, 2026 at 5:10 pm

    Since both your operands are integers, the compiler performs an integer division (that doesn’t calculate the decimal part). If at least one of the operands is a floating point type (as in your other examples) the other one gets promoted and a floating point division is performed.

    The fix

    Make at least one of the operands of a floating point type (double or float); you can do this e.g.:

    • making it a double literal2 (60 is integer, 60.0 or even 60. is double, 60.f is float)
    • using a cast (double(60), (double)60).

    Personally I prefer using directly double literals – not that the cast has any performance penalty on halfway decent compilers, but it feels “wrong” and verbose in respect to just using a literal of the correct type. (Obviously when both the operands are variables and not literals you have to use the cast)

    Common objections

    • “but I’m assigning it to a double!”

      Many newbies are confused by this fact, since they think that assigning1 the result to a double should be some kind of hint to the compiler. In fact, it’s not.

      The calculations/promotions done into the expression are completely independent from the type of the destination, which is just the last step. The subexpressions are evaluated for what they are, without regard to how the result will be used, so all the type promotions/operations depend only from the type of the operands.

    • why would one want integer division?

      Several languages automatically perform floating point division even when the arguments are both integers (e.g. VB6, IIRC), since it feels more intuitive for novices. That’s not like that in C/C++: the division is integral when the arguments are integer, since in many cases you just don’t care about the decimals, and/or it’s preferable for performance reasons not to use the FPU (the background philosophy in C and C++ is “you don’t pay for what you don’t use”).

      Obviously the problem could have been addressed using a separate operator for integral division (VB, again, uses \), but IMHO we have enough operators in C++ as it is. 🙂


    1. Nitpickers’ corner: yes, here actually it’s an initialization, not an assignment, but we are talking about the same kind of misconception.
    2. A “literal” is a value included in the source code.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Try this code - import java.io.StringReader; public class StringReaderTest { public static void main(String[]
Try this piece of code - public class WhitespaceTest { public static void main(String[]
The following code does not catch an exception, when I try to divide by
In the following program: int main(){ std::cout<<enter numbers to be divide<<std::endl; int a,b,c; while(true){
When I code stuff, I try to divide all things in functions (methods, if
try { JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++) { JSONObject json_data = jArray.getJSONObject(i);
I'm trying to catch a divide by 0 arithmetic exception in my code but
I am trying to use try-catch blocks to handle a divide-by-zero exception in marmalade,
I am trying to divide the values of DriveInfo.AvailableFreeSpace & DriveInfo.TotalSize to try and
I was trying this piece of code to check whether the divide by zero

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.