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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:17:41+00:00 2026-06-05T17:17:41+00:00

Can Anyone help me why x2 prints zero. I guess because of floating point

  • 0

Can Anyone help me why x2 prints zero.
I guess because of floating point representation X1 is rounded off, is there way to keep the precession.

long double x1, x2;
x1= 0.087912088; // Note: 360/4095 = 0.087912088
x2 = 360/4095;
printf("%Lf, %Lf \n",x1, x2);

Result:

x1 =0.087912
x2= 0.000000
  • 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-05T17:17:43+00:00Added an answer on June 5, 2026 at 5:17 pm

    The problem is integer truncation .. you are dividing two integers => the result will be another integer with the fractional part thrown away. (So for instance in the case when the real result of an integer division would be 3.9, truncation would make it 3 (ie. it doesn’t round)).

    In your case, if you change this to:

    x2 = 360 / 4095.0;  /* one of the operands now has a decimal point */
    

    you’ll get

    0.087912, 0.087912 
    

    as output.

    I.e., as soon as one or both of the operands of the division operator / are float/doubles, the result will be too (i.e., it will be “promoted” to float/double). So I could have changed x2 to

    x2 = 360.0 / 4095.0;
    

    or

    x2 = 360.0 / 4095;
    

    and would have gotten the same result as above.

    As mentioned by @chris just using a . is sufficient too.

    Re your question above about precision:

    You are already working with long doubles .. I don’t think internally you can change anything unless you use some special library, but you can certainly display more digits. E.g.,

      printf("%Lf, %.20Lf \n",x1, x2); 
    

    will yield

      0.087912, 0.08791208791208791895 
    

    Finally, as @edA-qa mort-ora-y reminds us, you can also cast values to certain types, but it matters when you do it. A simple example (note that the values end up as float after the assignment in any case since v is a float):

    float v = 0;
                         /* values shown are BEFORE assignment */
    v = (5 / 2);         /* value is 2 due to integer truncation before assignment */
    v = (float) (5 / 2); /* 2.0 as integer division occurs 1st, then cast to float */
    v = (float) 5 / 2;   /* 2.5 since 5 becomes 5.0 through casting first. */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone help me with sample code for a compass needle that points in
Can anyone help - this is driving me mad. I am calling a mysql
Can anyone help me how to split /explode verse format to 3 parts? The
Can anyone help me to get good WordPress interview questions and answers. Any link
Can anyone help me in converting scalar type of openCV to basic types like
can anyone help me for the exercise 12.5 of Jason Hickey's book? Basically, the
Can anyone help me with the script to refresh the page once . I
Can anyone help me find an up-to-date, working ATL project which has a main
Can anyone help or provide me with some suggestions for the below query. I
Can anyone help with this exersise? Write a program which asks the user for

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.