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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:14:13+00:00 2026-05-11T13:14:13+00:00

Why is the result of this explicit cast different from the implicit one? #include

  • 0

Why is the result of this explicit cast different from the implicit one?

#include <stdio.h>  double  a; double  b; double  c;  long    d;  double    e;  int main() {     a = 1.0;     b = 2.0;     c = .1;      d = (b - a + c) / c;     printf('%li\n', d);        //    10      e = (b - a + c) / c;     d = (long) e;     printf('%li\n', d);        //    11     } 

If I do d = (long) ((b – a + c) / c); I also get 10. Why does the assignment to a double make a difference?

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

    I suspect the difference is a conversion from an 80-bit floating point value to a long vs a conversion from an 80-bit floating point value to a 64-bit one and then a conversion to a long.

    (The reason for 80 bits coming up at all is that that’s a typical precision used for actual arithmetic, and the width of floating point registers.)

    Suppose the 80-bit result is something like 10.999999999999999 – the conversion from that to a long yields 10. However, the nearest 64-bit floating point value to the 80-bit value is actually 11.0, so the two-stage conversion ends up yielding 11.

    EDIT: To give this a bit more weight…

    Here’s a Java program which uses arbitrary-precision arithmetic to do the same calculation. Note that it converts the double value closest to 0.1 into a BigDecimal – that value is 0.1000000000000000055511151231257827021181583404541015625. (In other words, the exact result of the calculation is not 11 anyway.)

    import java.math.*;  public class Test {     public static void main(String[] args)     {         BigDecimal c = new BigDecimal(0.1d);                 BigDecimal a = new BigDecimal(1d);         BigDecimal b = new BigDecimal(2d);          BigDecimal result = b.subtract(a)                              .add(c)                              .divide(c, 40, RoundingMode.FLOOR);         System.out.println(result);     } } 

    Here’s the result:

    10.9999999999999994448884876874217606030632 

    In other words, that’s correct to about 40 decimal digits (way more than either 64 or 80 bit floating point can handle).

    Now, let’s consider what this number looks like in binary. I don’t have any tools to easily do the conversion, but again we can use Java to help. Assuming a normalised number, the ’10’ part ends up using three bits (one less than for eleven = 1011). That leaves 60 bits of mantissa for extended precision (80 bits) and 48 bits for double precision (64 bits).

    So, what’s the closest number to 11 in each precision? Again, let’s use Java:

    import java.math.*;  public class Test {     public static void main(String[] args)     {         BigDecimal half = new BigDecimal('0.5');                 BigDecimal eleven = new BigDecimal(11);          System.out.println(eleven.subtract(half.pow(60)));         System.out.println(eleven.subtract(half.pow(48)));             } } 

    Results:

    10.999999999999999999132638262011596452794037759304046630859375 10.999999999999996447286321199499070644378662109375 

    So, the three numbers we’ve got are:

    Correct value: 10.999999999999999444888487687421760603063... 11-2^(-60): 10.999999999999999999132638262011596452794037759304046630859375 11-2^(-48): 10.999999999999996447286321199499070644378662109375 

    Now work out the closest value to the correct one for each precision – for extended precision, it’s less than 11. Round each of those values to a long, and you end up with 10 and 11 respectively.

    Hopefully this is enough evidence to convince the doubters 😉

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

Sidebar

Related Questions

I want to get the last result returned from this query. How do I
Can someone explain this result to me. The first test succeeds but the second
I want do something like this: Result = 'MyString' in [string1, string2, string3, string4];
Is there a better way of getting this result? This function fails if num
I'm trying to determine, based on the result of this call, if it was
@search_results = Array.new duplicates = Set.new results.each { |result| @search_results.push(result) unless duplicates.add?(result[:url]) } This
Searching on Google reveals x2 code snippets. The first result is to this code
This question is a result of what i noticed while trying to answer another
Why does this test program result in a java.lang.IllegalMonitorStateException ? public class test {
I find this line in the ZenTest source code: result = @test_mappings.find { |file_re,

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.