[.net] Extracting mantissa out of float

Started by
12 comments, last by capn_midnight 19 years, 3 months ago
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.
Advertisement
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.
- k2"Choose a job you love, and you'll never have to work a day in your life." — Confucius"Logic will get you from A to B. Imagination will get you everywhere." — Albert Einstein"Money is the most egalitarian force in society. It confers power on whoever holds it." — Roger Starr{General Programming Forum FAQ} | {Blog/Journal} | {[email=kkaitan at gmail dot com]e-mail me[/email]} | {excellent webhosting}
where did you get that? :)
Quote:Original post by RipTorn
where did you get that? :)
most of .net is open source

[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]

This topic is closed to new replies.

Advertisement