• Advertisement
Sign in to follow this  

[.net] Extracting mantissa out of float

This topic is 4759 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, in C# how can i get the mantissa bit pattern from a float uint a = (f & 0x007fffff); wont compile because operator & cant be applied to a float. Cheers Yratelev

Share this post


Link to post
Share on other sites
Advertisement
Converting a float to an int by preserving bit pattern:
float f = (some float number);
unsigned int i = *(int*) &f;

Now you can just use the bitwise stuff to get whichever part of the float you want.

[EDIT] Oh, it's C#...eh, not sure if what I wrote is valid without using an unsafe block.

Share this post


Link to post
Share on other sites
yeh i knew how a float was constructed, just needed to know how to extract its bit pattern, thnx now!

Yratelev

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
Converting a float to an int by preserving bit pattern:
float f = (some float number);
unsigned int i = *(int*) &f;

Now you can just use the bitwise stuff to get whichever part of the float you want.

[EDIT] Oh, it's C#...eh, not sure if what I wrote is valid without using an unsafe block.

wouldn't it be safer to use a union of a float and an int?

Share this post


Link to post
Share on other sites
Quote:
Original post by capn_midnight
Quote:
Original post by Promit
Converting a float to an int by preserving bit pattern:
float f = (some float number);
unsigned int i = *(int*) &f;

Now you can just use the bitwise stuff to get whichever part of the float you want.

[EDIT] Oh, it's C#...eh, not sure if what I wrote is valid without using an unsafe block.

wouldn't it be safer to use a union of a float and an int?

C# doesn't have unions. A list of C# keywords is here. You can use the FixedLayout attribute to emulate them, though.

Share this post


Link to post
Share on other sites
Is Longhorn supposed to give a warning about your program if it uses unsafe code?

Share this post


Link to post
Share on other sites

float f=...;

int i = System.BitConverter.ToInt32( System.BitConverter.GetBytes(f) , 0) & 0x007fffff;

Share this post


Link to post
Share on other sites
Quote:
Original post by RipTorn

float f=...;

int i = System.BitConverter.ToInt32( System.BitConverter.GetBytes(f) , 0) & 0x007fffff;

That looks good but will it be much slower than the unsafe version?

Share this post


Link to post
Share on other sites
Quote:
Original post by DrGUI
Quote:
Original post by RipTorn
...

That looks good but will it be much slower than the unsafe version?


That I cannot say.
I'm not sure if GetBytes makes a copy of the byte data or just returns a wrapper Array on the float.
Logically, the JIT should optimize it down so it's just as fast.
But then again I don't know anything about the way the JIT optimises. If it wasn't summer I could probably ask one of the guys I know working on Rotor's optimiser... But for now it's just a guess. Sorry.

I don't think there would be any other way to do it in safe code however.
So I guess it's just a question of if you want it safe or not.

You could do what I do an have #define USE_UNSAFE_CODE then appropriate #if #else #endif blocks.

profile it with nprof and see I guess.

my experience with .net so far has mostly shown that things you expect to be slow end up being very fast, and things you expect to be fast end up slow :) so you never know, it might be faster than the unsafe version ;) - or the unsafe version may just compile to the same code anyway.

Share this post


Link to post
Share on other sites
Quote:
Original post by RipTorn
Quote:
Original post by DrGUI
Quote:
Original post by RipTorn
...

That looks good but will it be much slower than the unsafe version?


That I cannot say.
I'm not sure if GetBytes makes a copy of the byte data or just returns a wrapper Array on the float.


Here's the implementation for System.BitConverter.GetBytes(float value) in .NET 1.1:


00120 // Converts a float into an array of bytes with length
00121 // four.
00123 public unsafe static byte[] GetBytes(float value)
00124 {
00125 byte[] bytes = new byte[4];
00126 fixed(byte* b = bytes)
00127 *((float*)b) = value;
00128 return bytes;
00129 }




Given that, it should be quite fast.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement