std::isnormal in C#

Soonts 07/11/2018. 2 answers, 347 views
c# floating-point

Is this correct? Is this portable to .NET core and 32-bit ARM Linux? Is there a faster way, maybe something in the .NET framework? Is there a way to implement float32 version without using dynamic memory i.e. without byte arrays?

/// <summary>Determines if the given floating point number is normal, i.e. is neither zero, subnormal, infinite, nor NaN.</summary>
public static bool isNormal( this double v )
{
    long i64 = BitConverter.DoubleToInt64Bits( v );
    long expMask = 0x7ff0000000000000L;
    long expBits = i64 & expMask;
    return expBits != 0 && expBits != expMask;
}

/// <summary>Determines if the given floating point number is normal, i.e. is neither zero, subnormal, infinite, nor NaN.</summary>
public static bool isNormal( this float v )
{
    byte[] bytes = BitConverter.GetBytes( v );
    uint i32 = BitConverter.ToUInt32( bytes, 0 );
    uint expMask = 0x7f800000;
    uint expBits = i32 & expMask;
    return expBits != 0 && expBits != expMask;
}

2 Answers


Adriano Repetti 07/13/2018.

Code is formally correct, just change the name to IsNormal() (PascalCase) to follow standard .NET naming conventions.

However your second question "Is this portable to .NET core and 32-bit ARM Linux..." is little bit more tricky. In normal conditions you should almost never care about host endianness but floating point numbers have exceptions on ARM (and possibly some other architectures which, however, don't run .NET Core). Specifically:

  • ARM EABI does not store floating point numbers using the natural-endianness but a mixed-endian convention. You can't compare BitConverter.GetBytes(v) result with an int because they're surely encoded differently.
  • CirrusLogic Maverick (formally EP9312) is an ARM9 System-on-chip with a MaverickCrunch math cooprocessor where floating point numbers are always little-endian. You can't compare BitConverter.GetBytes(v) result with an int because they may be encoded differently.

Honestly, regardless the CPU, I am not sure about Linux images compiled with software floating point support then (if you need to support them) you have to try (or find related documentation).

The good news is that you probably won't ever need to deal with them. The bad news is that, if you really need to, compiling for AnyCPU you have to detect endianness at run-time (and ARM CPUs may be configured...)


Henrik Hansen 07/13/2018.

You ask for another way to do this - using the .net framework (I have no experience with Linux, so don't hang me if it doesn't work there).

1)

public static bool IsNormal1(float v)
{
  return !float.IsInfinity(v) && !float.IsNaN(v) && Math.Abs(v) >= 1.175494351E-38;  
}

The magic number 1.175494351E-38 is discussed here.


2) If you are willing to go unsafe you could do this:

const int floatExpMask = 0x7F800000;
public unsafe static bool IsNormal2(float f)
{
  return
    (*(int*)(&f) & 0x7FFFFFFF) < floatExpMask // Infinity and NaN
    && (*(int*)(&f) & floatExpMask) != 0; // Subnormal and zero
}

HighResolutionMusic.com - Download Hi-Res Songs

1 The Chainsmokers

Beach House flac

The Chainsmokers. 2018. Writer: Andrew Taggart.
2 (G)I-DLE

POP/STARS flac

(G)I-DLE. 2018. Writer: Riot Music Team;Harloe.
3 Ariana Grande

​Thank U, Next flac

Ariana Grande. 2018. Writer: Crazy Mike;Scootie;Victoria Monét;Tayla Parx;TBHits;Ariana Grande.
4 Anne-Marie

Rewrite The Stars flac

Anne-Marie. 2018. Writer: Benj Pasek;Justin Paul.
5 Clean Bandit

Baby flac

Clean Bandit. 2018. Writer: Jack Patterson;Kamille;Jason Evigan;Matthew Knott;Marina;Luis Fonsi.
6 Nicki Minaj

No Candle No Light flac

Nicki Minaj. 2018. Writer: Denisia “Blu June” Andrews;Kathryn Ostenberg;Brittany "Chi" Coney;Brian Lee;TJ Routon;Tushar Apte;ZAYN;Nicki Minaj.
7 BlackPink

Kiss And Make Up flac

BlackPink. 2018. Writer: Soke;Kny Factory;Billboard;Chelcee Grimes;Teddy Park;Marc Vincent;Dua Lipa.
8 Imagine Dragons

Bad Liar flac

Imagine Dragons. 2018. Writer: Jorgen Odegard;Daniel Platzman;Ben McKee;Wayne Sermon;Aja Volkman;Dan Reynolds.
9 BTS

Waste It On Me flac

BTS. 2018. Writer: Steve Aoki;Jeff Halavacs;Ryan Ogren;Michael Gazzo;Nate Cyphert;Sean Foreman;RM.
10 Halsey

Without Me flac

Halsey. 2018. Writer: Halsey;Delacey;Louis Bell;Amy Allen;Justin Timberlake;Timbaland;Scott Storch.
11 Little Mix

Woman Like Me flac

Little Mix. 2018. Writer: Nicki Minaj;Steve Mac;Ed Sheeran;Jess Glynne.
12 Brooks

Limbo flac

Brooks. 2018.
13 Fitz And The Tantrums

HandClap flac

Fitz And The Tantrums. 2017. Writer: Fitz And The Tantrums;Eric Frederic;Sam Hollander.
14 Backstreet Boys

Chances flac

Backstreet Boys. 2018.
15 Lady Gaga

I'll Never Love Again flac

Lady Gaga. 2018. Writer: Benjamin Rice;Lady Gaga.
16 Diplo

Close To Me flac

Diplo. 2018. Writer: Ellie Goulding;Savan Kotecha;Peter Svensson;Ilya;Swae Lee;Diplo.
17 Rita Ora

Velvet Rope flac

Rita Ora. 2018.
18 Bradley Cooper

Always Remember Us This Way flac

Bradley Cooper. 2018. Writer: Lady Gaga;Dave Cobb.
19 Imagine Dragons

Machine flac

Imagine Dragons. 2018. Writer: Wayne Sermon;Daniel Platzman;Dan Reynolds;Ben McKee;Alex Da Kid.
20 Erika Sirola

Speechless flac

Erika Sirola. 2018. Writer: Teemu Brunila;Stefan Dabruck;Jürgen Dohr;Guido Kramer;Dennis Bierbrodt;Chris Braide;Robin Schulz.

Related questions

Hot questions

Language

Popular Tags