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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:50:10+00:00 2026-06-16T00:50:10+00:00

Given the statements float f = 7.1f; double d = f; What can we

  • 0

Given the statements

float f = 7.1f;
double d = f;

What can we assert in a unit test about d?


For example this does not work:

Console.WriteLine(d == 7.1d); // false
Console.WriteLine(d < 7.1d + float.Epsilon); // true by luck
Console.WriteLine(d > 7.1d - float.Epsilon); // false (less luck)

The best way I found so far is to convert the value back:

float f2 = (float)d;
Console.WriteLine(f2 == f); // true

Which would be the same as the brute way to say

Console.WriteLine(d == 7.1f); // 7.1f implicitly converted to double as above

This question is NOT about double and float precision in general but really JUST about the pragmatic question how a unit test can best describe the confines of d. In my case, d is the result of a conversion that occurs in code generated by light weight code generation. While testing this code generation, I have to make assertions about the outcome of this function and this finally boils down to the simple question above.

  • 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-16T00:50:12+00:00Added an answer on June 16, 2026 at 12:50 am

    Your “best way” is asserting that your generated code returns something that is, within float‘s margin of error, 7.1. This may be what you want to check, in which case, carry on.

    On the other hand, you might want to assert that your generated code returns specifically the result of casting 7.1f to a double, in which case you could do:

    Console.WriteLine(d == (double)f);
    

    This is more stringent – your test asserts that d is within a small range, while the above test asserts that d is a specific value.

    It really depends on what you’ll be using d for. If it’s a case where things will go wrong if it’s not the exact value, test the exact value, but if it’s OK to be within a float of the value, check against the float.

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

Sidebar

Related Questions

Given a list a = range(10) You can slice it using statements such as
Given the following method: def some_method :value end The following statements work as I
Given a vector such as this: struct product { float price; float shipping; };
Given a real (n), a maximum value this real can be (upper), and a
I have a specification which essentially looks like this: def my example specification(){ given:some
Given the following statements in VBA: Assume that val = 4 and Option Base
Both in Actionscript3 and Javascript these statements give the same result: /\S/.test(null) => true
Problem Statement is : Given 2 Dimensional array, print output for example If 4
Given an XSL 'If' statement: <xsl:if test=a = 'some value' and b = 'another
How can you write the following statement in the given languages? a(0) = 1

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.