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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:13:46+00:00 2026-06-13T23:13:46+00:00

Our existing application reads some floating point numbers from a file. The numbers are

  • 0

Our existing application reads some floating point numbers from a file. The numbers are written there by some other application (let’s call it Application B). The format of this file was fixed long time ago (and we cannot change it). In this file all the floating point numbers are saved as floats in binary representation (4 bytes in the file).

In our program as soon as we read the data we convert the floats to doubles and use doubles for all calculations because the calculations are quite extensive and we are concerned with the spread of rounding errors.

We noticed that when we convert floats via decimal (see the code below) we are getting more precise results than when we convert directly. Note: Application B also uses doubles internally and only writes them into the file as floats. Let’s say Application B had the number 0.012 written to file as float. If we convert it after reading to decimal and then to double we get exactly 0.012, if we convert it directly, we get 0.0120000001043081.

This can be reproduced without reading from a file – with just an assignment:

    float readFromFile = 0.012f;
    Console.WriteLine("Read from file: " + readFromFile); 
    //prints 0.012

    double forUse = readFromFile;
    Console.WriteLine("Converted to double directly: " + forUse);
    //prints 0.0120000001043081

    double forUse1 = (double)Convert.ToDecimal(readFromFile);
    Console.WriteLine("Converted to double via decimal: " + forUse1); 
    //prints 0.012

Is it always beneficial to convert from float to double via decimal, and if not, under what conditions is it beneficial?

EDIT: Application B can obtain the values which it saves in two ways:

  1. Value can be a result of calculations
  2. Value can be typed in by user as a decimal fraction (so in the example above the user had typed 0.012 into an edit box and it got converted to double, then saved to float)
  • 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-13T23:13:46+00:00Added an answer on June 13, 2026 at 11:13 pm

    As strange as it may seem, conversion via decimal (with Convert.ToDecimal(float)) may be beneficial in some circumstances.

    It will improve the precision if it is known that the original numbers were provided by users in decimal representation and users typed no more than 7 significant digits.

    To prove it I wrote a small program (see below). Here is the explanation:

    As you recall from the OP this is the sequence of steps:

    1. Application B has doubles coming from two sources:
      (a) results of calculations; (b) converted from user-typed decimal numbers.
    2. Application B writes its doubles as floats into the file – effectively
      doing binary rounding from 52 binary digits (IEEE 754 single) to the 23 binary digits (IEEE 754 double).
    3. Our Application reads that float and converts it to a double by one of two ways:

      (a) direct assignment to double – effectively padding a 23-bit number to a 52-bit number with binary zeros (29 zero-bits);

      (b) via conversion to decimal with (double)Convert.ToDecimal(float).

    As Ben Voigt properly noticed Convert.ToDecimal(float) (see MSDN in the Remark section) rounds the result to 7 significant decimal digits. In Wikipedia’s IEEE 754 article about Single we can read that precision is 24 bits - equivalent to log10(pow(2,24)) ≈ 7.225 decimal digits. So, when we do the conversion to decimal we lose that 0.225 of a decimal digit.

    So, in the generic case, when there is no additional information about doubles, the conversion to decimal will in most cases make us loose some precision.

    But (!) if there is the additional knowledge that originally (before being written to a file as floats) the doubles were decimals with no more than 7 digits, the rounding errors introduced in decimal rounding (step 3(b) above) will compensate the rounding errors introduced with the binary rounding (in step 2. above).

    In the program to prove the statement for the generic case I randomly generate doubles, then cast it to float, then convert it back to double (a) directly, (b) via decimal, then I measure the distance between the original double and the double (a) and double (b). If the double(a) is closer to the original than the double(b), I increment pro-direct conversion counter, in the opposite case I increment the pro-viaDecimal counter. I do it in a loop of 1 mln. cycles, then I print the ratio of pro-direct to pro-viaDecimal counters. The ratio turns out to be about 3.7, i.e. approximately in 4 cases out of 5 the conversion via decimal will spoil the number.

    To prove the case when the numbers are typed in by users I used the same program with the only change that I apply Math.Round(originalDouble, N) to the doubles. Because I get originalDoubles from the Random class, they all will be between 0 and 1, so the number of significant digits coincides with the number of digits after the decimal point. I placed this method in a loop by N from 1 significant digit to 15 significant digits typed by user. Then I plotted it on the graph. The dependency of (how many times direct conversion is better than conversion via decimal) from the number of significant digits typed by user.
    The dependency of (how many times direct conversion is better than via decimal) from the number of significant digits typed by user.

    As you can see, for 1 to 7 typed digits the conversion via Decimal is always better than the direct conversion. To be exact, for a million of random numbers only 1 or 2 are not improved by conversion to decimal.

    Here is the code used for the comparison:

    private static void CompareWhichIsBetter(int numTypedDigits)
    {
        Console.WriteLine("Number of typed digits: " + numTypedDigits);
        Random rnd = new Random(DateTime.Now.Millisecond);
        int countDecimalIsBetter = 0;
        int countDirectIsBetter = 0;
        int countEqual = 0;
    
        for (int i = 0; i < 1000000; i++)
        {
            double origDouble = rnd.NextDouble();
            //Use the line below for the user-typed-in-numbers case.
            //double origDouble = Math.Round(rnd.NextDouble(), numTypedDigits); 
    
            float x = (float)origDouble;
            double viaFloatAndDecimal = (double)Convert.ToDecimal(x);
            double viaFloat = x;
    
            double diff1 = Math.Abs(origDouble - viaFloatAndDecimal);
            double diff2 = Math.Abs(origDouble - viaFloat);
    
            if (diff1 < diff2)
                countDecimalIsBetter++;
            else if (diff1 > diff2)
                countDirectIsBetter++;
            else
                countEqual++;
        }
    
        Console.WriteLine("Decimal better: " + countDecimalIsBetter);
        Console.WriteLine("Direct better: " + countDirectIsBetter);
        Console.WriteLine("Equal: " + countEqual);
        Console.WriteLine("Betterness of direct conversion: " + (double)countDirectIsBetter / countDecimalIsBetter);
        Console.WriteLine("Betterness of conv. via decimal: " + (double)countDecimalIsBetter / countDirectIsBetter );
        Console.WriteLine();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can we move our existing ASP .Net 2.0 application to the cloud platform
From our existing, internal tracking system I would like to create an XML export
I think we may have trouble with our existing project. For some reasons we
We want to update an existing Application but unfortunately we have a problem. Our
My company's main application is mostly written in C++ (with some Delphi code and
Planning to migrate our existing application to Azure. Our existing architecture with security flow
I have a Java application that I need to integrate our existing PHP website
We have an existing application for which one of our DTO object has a
I'm doing a little research on possible application of EWS in our existing project
We have an application that reads in a flat file and parses out the

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.