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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:26:14+00:00 2026-06-15T15:26:14+00:00

double n = 1.3243; for (int i = 0; long(n*10) % 10 != 0;

  • 0
double n = 1.3243;
for (int i = 0; long(n*10) % 10 != 0; i++, n *= 10) {

}

I’ve written this code in order to understand whether a number has a decimal part or not.

At the end of the loop ‘i’ should be 4 but for some reason the counter doesn’t increment.

Except for the fact that you may not like my solution, do you have any suggestions?

  • 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-15T15:26:15+00:00Added an answer on June 15, 2026 at 3:26 pm

    At the end of your loop, the i-variable does no longer exist. You’re declaring it inside the function scope. This results in not being able to access it outside the loop. If you do something like:

    double n = 1.3243;
    for (int i = 0; long(n*10) % 10 != 0; i++, n *= 10) {   
    }
    printf("%d\n", i);
    

    GGC gives me a loop.cpp:10: error: name lookup of ‘i’ changed for new ISO ‘for’ scoping error.

    The following would fix that (notice how the for-loop does not need curly brackets if it’s empty):

    double n = 1.3243;
    int i;
    for (i = 0; long(n*10) % 10 != 0; i++, n *= 10);
    printf("%d\n", i);
    

    Using GCC 4.2.1, this gives me the output of 4

    But the loop you presented has an inconvenient bug, when testing for decimal numbers. As src remarked in his comment, a zero in the decimals cancels any decimals behind it. The loop simply breaks off as soon as it finds a zero value, but there could be more decimals following. Depending on the type of floats you’re dealing with, this could be quite a problem.

    Do note: The most common solution is the following comparison:

    double n = 1.3243;
    if (n == (int)n) {
        // do stuff
    }
    

    This fixes the error presented by src: 1.01 is said to have decimals, like it should. The loop solution (wrongly) returns i=0 for this float.

    The comparison n == (int)n returns true when your float does not have any decimal parts.

    As H2CO3 mentioned, testing the decimals of a float-point number is not 100% definite, as floating point rounding might give you a slightly different answer than you’re expecting. It works in the general case, though, so you’ll have to see for yourself if it fits the problem you’re solving.

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

Sidebar

Related Questions

double d = 0; // random decimal value with it's integral part within the
Getting double free for below code, if a long string passed. I tried all
Code: double x(){return (double)rand()/(double)RAND_MAX;} double y(){return (double)rand()/(double)RAND_MAX;} double z(){return (double)rand()/(double)RAND_MAX;} int d(double x, double
double p1::root(double (*pf)(double k), int a, int b, double e) im not sure how
double? test = true ? null : 1.0; In my book, this is the
double d[10]; int length = 10; memset(d, length * sizeof(double), 0); //or for (int
Double has range more than a 64-bit integer, but its precision is less dues
Using long double I get 18/19 = 0.947368421052631578... , and 947368421052631578 is the repeating
double s_deviation(double data[],int cnt, double mean) { int i; double sum= 0; double sdeviation;
double dval = 1; for (int i = 0; i < Cols; i++) {

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.