c to c# code conversion with regards to floating format

0x0-dot-co-dot-za 1 Reputation point
2022-02-13T09:42:31.127+00:00

I have the following 2 lines of code in c

1) #define FLT_EPSILON 0x1p-23
2) #define FLT_MAX 0x1.fffffep+127

The first line can be converted to C# as follows without any syntax errors:

  1. public float FLT_EPSILON = 0x1E-23F;

However, I'm struggling with (2). I'm aware of things like Float.MaxValue etc.. but is there a way to express it to closely resemble the c code? Its basically expressing floating point values in hex format unless Im mistaken.
2) #define FLT_MAX 0x1.fffffep+127

Developer technologies | C#
{count} votes

3 answers

Sort by: Most helpful
  1. Castorix31 90,686 Reputation points
    2022-02-13T10:31:59.827+00:00

    In C, in float.h, I have :

    #define FLT_MAX          3.402823466e+38F        // max value
    

    same thing in C# :

     public float FLT_MAX = 3.402823466e+38F;
    
    0 comments No comments

  2. Anonymous
    2023-10-21T18:18:05.5866667+00:00

    Format code #C

    0 comments No comments

  3. Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
    2023-10-21T20:29:53.5733333+00:00

    Floating point number are in binary. This translates to hex fine, but there are precision errors when decimal is used. This why hex was added to c.

    You should use the defined min, max and epsilon values in c#

    https://learn.microsoft.com/en-us/dotnet/api/system.single.epsilon?view=net-7.0

    which the compiler guarantees will be the correct binary values.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.