Thread: FPN math

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,735

    FPN math

    Some of you will already know this but I'm working on a function for a compiler I'm making, this function is supposed to read both integers and floating numbers based on text provided. I was struggling to identify what variables I needed where and how to handle them, I then thought to do a small project/file with fixed data to verify those details, thing is I've now hit a roadblock and struggling to come up with ideas of how to fix it, I think my problem lies in how I calculate the mantissa (parameter man) but it could also be the exponent (parametet exp) or both, I started with the floating part as 0 to check the exponent so I don't think it's that though. Here's what I have so far:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <inttypes.h>
    #include <float.h>
    typedef unsigned char uchar;
    typedef unsigned long long ullong;
    #define TYPE float
    typedef union LDBL {
    	TYPE fpn;
    	uchar hex[sizeof(TYPE)];
    	struct {
    		ulong man : 23;
    		ulong exp : 8;
    		ulong sig : 1;
    	};
    } LDBL;
    
    void print( char *text, LDBL val ) {
    	printf("%s",text);
    	for ( size_t i = 0; i < sizeof(TYPE); ++i ) {
    		printf( " %02X", val.hex[i] );
    	}
    }
    #define NUM 2
    #define FPN 0
    int main() {
    	ulong num = 3, fpn = 0, exp = 0;
    	ulong bit = 1, pos = 0;
    	LDBL test = {0}, mine = {0};
    	test.fpn = fpn;
    	test.fpn /= 10;
    	test.fpn += num;
    	printf("%E\n", test.fpn);
    	for ( pos = 0; bit; bit <<= 1 ) {
    		mine.man |= bit;
    		if ( num & bit ) mine.exp = pos;
    		++pos;
    		if ( pos == FLT_MAX_EXP ) break;
    	}
    	mine.exp += FLT_MAX_EXP;
    	if ( exp )
    		mine.exp = (mine.exp == (FLT_MAX_EXP) * 2) ? -1 : mine.exp + exp;
    	num *= 10;
    	bit = 1;
    	pos = 0;
    	if ( mine.exp != (FLT_MAX_EXP + exp) )
    		for ( ; !(bit & num); bit <<= 1, ++pos );
    	for ( ; pos < 23; ++pos ) {
    		fpn <<= 1;
    		mine.man <<= 1;
    		if ( !fpn ) break;
    		mine.man |= (fpn & bit);
    	}
    	print( "test", test );
    	putchar('\n');
    	print( "mine", mine );
    	putchar('\n');
    	printf( "%08X %08X\n", test.exp, mine.exp );
    	return 0;
    }
    I'll take a look at some bugs mentioned of mitsy before taking a break until I think of something or someone else manages to think of something (and posts it)

    Edit: Oh and here are my results:
    Code:
    gcc -Wall -o "test_fpn" "test_fpn.c" && "./test_fpn"
    3.000000E+00
    test 00 00 40 40
    mine FE FF FF 40
    00000080 00000081
    Compilation finished successfully.
    Last edited by awsdert; 10-29-2019 at 03:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ and Math
    By darren78 in forum C++ Programming
    Replies: 2
    Last Post: 07-08-2010, 09:19 AM
  2. hex math
    By kroiz in forum C Programming
    Replies: 25
    Last Post: 01-20-2009, 03:46 PM
  3. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  4. math.h
    By sweets in forum C++ Programming
    Replies: 2
    Last Post: 05-05-2003, 01:27 PM
  5. Math Help
    By CAP in forum C Programming
    Replies: 2
    Last Post: 08-19-2002, 12:03 AM

Tags for this Thread