[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: floating point hex calc?



Indeed I did eventually just write one.  It's kinda lame, but the code follows.

The fancy part is casting an integer to a float without altering the bits. Doing a simple cast makes a floating point value that is numerically equal to the hex integer, rather than having the same bits. So I took the address of the integer and cast that to a float*.

I think reinterpret_cast in C++ would have done the right thing.

Here's the source:

#include <stdio.h>

int main( int argc, char **argv )
{
	char buf[ 256 ];

	printf( "MyCalc: " );

	float foo;

	unsigned long bar;

	scanf( "%x", &bar );

	foo = *(float*)&bar;

	printf( "%1.15f\n", foo );

	return 0;
}

--
Michael D. Crawford
GoingWare Inc. - Expert Software Development and Consulting
http://www.goingware.com/
crawford@goingware.com

     Tilting at Windmills for a Better Tomorrow.



Reply to: