Is there a floatN_t type ?


 
Thread Tools Search this Thread
Top Forums Programming Is there a floatN_t type ?
# 1  
Old 06-05-2009
Is there a floatN_t type ?

Hi,

I'd like to know if there is any floatN_t type available in linux (glibc) to handle
float data of different size like it's done for integers (uintN_t in stdint.h).

I need to parse the payload of a message whose type is float, but whose size
in bit can be 8, 16, 32, 64 or 128.

Any suggestions?

Thanks,
S.
# 2  
Old 06-05-2009
There are 3 floating point types in C, as of C99:

float
double
long double

There are minimum sizes for these. The size for float on many machines is 32 bits, and for double 64 bits. On one of my machines, sizeof (long double) == 16 (128 bits).

I don't think glibc defines any new ones.

None of this is stopping you from doing your own floating point type representations using 8 or 16 bits, but you'll have to do it in the integer space and convert it. This isn't really that hard.

If you're passing these around on a network, you need to be very careful about the binary representations of these things, because there may be different floating point representations than what you're expecting.

HTH
# 3  
Old 06-05-2009
Quote:
Originally Posted by mgessner
There are 3 floating point types in C, as of C99:

float
double
long double

There are minimum sizes for these. The size for float on many machines is 32 bits, and for double 64 bits. On one of my machines, sizeof (long double) == 16 (128 bits).

I don't think glibc defines any new ones.
I can't rely on the common size used. They must be 8,16, etc.. bits

Quote:
Originally Posted by mgessner
None of this is stopping you from doing your own floating point type representations using 8 or 16 bits, but you'll have to do it in the integer space and convert it. This isn't really that hard.

If you're passing these around on a network, you need to be very careful about the binary representations of these things, because there may be different floating point representations than what you're expecting.

HTH
I think I'm going with my own representation, although a ready-to-use would
have been appreciated. Do you have any link/suggestion?

Up to 64 bits, I could typedef uintN_t to floatN_t for containing the variable.
While for float128_t I was thinking of
Code:
typedef struct {
   float64_t low;
   float64_t high;
} float128_t;

but as for interpreting and producing a value I'm not sure about how to do it.

And no, I'm not passing them around networks.

Thanks,
S.
# 4  
Old 06-05-2009
Quote:
Originally Posted by emitrax
I can't rely on the common size used. They must be 8,16, etc.. bits
Using anything other than the compiler types would mean writing all your own floating point routines completely from scratch, they wouldn't work with things like printf, and you'd discard all the performance benefits of using floating point types your CPU directly supports.

Neither does sticking two 64-bit floating point numbers together a 128-bit floating point make. They're not like integers. They've got structure inside. What you'd do to make your own custom floating point types is make a bitfield structure(integers, essentially) and routines to use it.

But an 8-bit floating point number implies you don't really care that much about accuracy. Might fixed-point numbers work instead? They can't encode NaN or infinity, but they are very straightforward to use, being just integers with an assumed decimal point...
# 5  
Old 06-05-2009
Emitrax -
IEEE 754-2008 - Wikipedia, the free encyclopedia
Try reading this carefully. You cannot go creating roll-your-own floating point numbers.
What are you really trying to do? The answer to this is NOT
'I want oddball FP datatypes'

Corona has it right - you are in for world of hurt trying to roll your own. Based on your earlier questions, I'll bet custom FP datatypes are not what you want or can program with, either.
# 6  
Old 06-05-2009
First of all, thanks to all for your answers.

What I'm trying to do here, is to deal with some data
defined to be float with variable size (based on some other
flags of course). As I said already, this float data can be
either 8, 16, 32, 64 or 128 bit large. Don't ask me how.
That's just what the specification I'm dealing with says.

So my problem is, using a variable for containing and
interpreting this data.

How should I cope with it? I've googled but found nothing.

Thanks for your help,
S.
# 7  
Old 06-05-2009
Embedded systems (are you writing in that environment?) sometimes use minifloat floating point objects. If I recall correctly the smallest is 16 bit, but I don't remember exactly.

I am assuming your spec did not define the internal format of your FP numbers, so minifloat is the only thing I have ever heard about. Try google for 'minifloat floating point'. There is a small library for it as well, I think.

None of this is standard C at all.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Changing type name

In C++, how can I change the type with another name For example How can I declaring an object real which would be the same as declaring it float (5 Replies)
Discussion started by: kristinu
5 Replies

2. Windows & DOS: Issues & Discussions

Type of RAM

Hello All Is there a way I can find out the type of RAM (DDR1 or DDR2 or DDR3), I'm using withoout opening the cabinet? Any Windows command? Thanks in advance. (5 Replies)
Discussion started by: tenderfoot
5 Replies

3. UNIX for Dummies Questions & Answers

key_t type

I would like to print value of key generate like this.... key_t k = ftok(".",'c'); printf("key is %d \n" , k); Is there other way to print this type. I dont think I am getting right output which is key is 1662074913 (shouldn't it be 6362074913) since 'c' is 63 in hex (ascii table).... (2 Replies)
Discussion started by: joker40
2 Replies

4. UNIX for Dummies Questions & Answers

Encoding Type

Hi, Where can I find the encoding type in a unix server ? Thanks in advance !!! (1 Reply)
Discussion started by: risshanth
1 Replies

5. Solaris

raid type

how to get the raid type of a mount if I am using solaris disksuite? thanks (1 Reply)
Discussion started by: melanie_pfefer
1 Replies

6. Programming

array type has incomplete element type

Dear colleagues, One of my friend have a problem with c code. While compiling a c program it displays a message like "array type has incomplete element type". Any body can provide a solution for it. Jaganadh.G (1 Reply)
Discussion started by: jaganadh
1 Replies

7. Shell Programming and Scripting

String type to date type

Can one string type variable changed into the date type variable. (1 Reply)
Discussion started by: rinku
1 Replies

8. Shell Programming and Scripting

Different type of shells?

Hello there, i just want to know what are the different type of shells and the main difference betwee them. The problem is that if you try to search over the net you will find a lot of information and hence you will have no enough time to read all of them.....Anyone can help with this?? (1 Reply)
Discussion started by: charbel
1 Replies

9. UNIX for Dummies Questions & Answers

you have more and one unix type?

As a formem unix newbe I Just wanted to tell you about this cool site for all you confused people. If you need to support more than one unix type: Use this one. It's a life saver. http://www.unixguide.net/unixguide.shtml here (4 Replies)
Discussion started by: sunbird
4 Replies
Login or Register to Ask a Question