Decomposing a FLOAT

I

Ian Collins

Thanks. I've never used the tgmath library before - can you give me any
pointers where to download it? What are the advantages over the standard
math library?

It's a standard header for generic maths functions.
 
I

Ian Collins

Raj Pashwar said:
On Sat, 02 Jun 2012 14:54:09 -0700, Keith Thompson wrote: [...]
Whatever the OP meant, I hope he understands by now that referring to
the type "float" as "FLOAT" is not a good idea.

Yes, thanks :)

In fact John Kupyer was right : on my system FLOAT is a type-def for
long double.

(James Kuyper)

Out of curiosity, where exactly does this typedef (not "type-def")
appear? Is it in a header for some particular application or library,
or is it really defined by your (operating) system?

FLOAT is a really bad name for a typedef. Either it's a typedef for the
built-in type "float", in which case it's fairly useless, or it's a
typedef for something other than "float", in which case it's actively
misleading.

(The only reasonable justification I can think of is that FLOAT might be
used to implement a type in some other language.)

Yes, it's in one of the headers.

I think the idea is: it gives flexibility to change between float (=small
size, low precision) or long double (=high size, high precision) or
double (=compromise) at a later stage, without needing to change all
definitions everywhere in code : only the typeDef needs changing.

You miss the point completely. The technique is common, it is the name
you have chosen that's bad.
 
E

Eric Sosman

[...]
In fact John Kupyer was right : on my system FLOAT is a type-def for
long double.

In that case, don't use frexpf(): Use frexpl() instead, or
#include<tgmath.h> and use frexp().

Thanks. I've never used the tgmath library before - can you give me any
pointers where to download it? What are the advantages over the standard
math library?

"Type-generic math" was added to C in the C99 Standard, toward
the end of the Clinton Administration. If you're using a pre-C99
implementation you might not have it, in which case you should use
frexpl() explicitly.

The <tgmath.h> standard header provides magical macros that
inspect their argument types and figure out whether to call the
float, double, or long double math function. Using <tgmath.h>, you
can just write frexp(x) and the compiler will inspect `x' to
decide whether to use frexpf, frexp (the actual function taking
double), or frexpl.

Your situation is exactly the kind of thing that motivated
<tgmath.h> in the first place. It's easy enough to write a call
to sqrtf() for a float argument or sqrtl() for a long double,
but when the type is hidden behind a typedef it's not so clear
which function to apply. In your case the visible name of the
type was FLOAT, so we all directed you to the frexp() function,
which is appropriate for a float argument. With <tgmath.h>, the
compiler will figure this out for you, even if the visible type
name is REAL or SCALAR: The compiler knows whether these resolve
to float or double or long double, and uses the appropriate
function.

The underlying functions are in <math.h>, and you can call
them by their full names if you desire (or if you must, lacking
a C99 implementation).

The latest "C11" Standard introduces machinery to let user-
written functions do similar magic, but I haven't tried it yet.
 
J

James Kuyper

. ....
Yes, it's in one of the headers.

I think that when he asked "where exactly", he was looking for an
answer more specific than "yes".
Is it a header for some particular application - if so, which one?
Is it a header for a library - if so, which one?
Is it a header for your operating system - if so, which one?
 
K

Keith Thompson

Raj Pashwar said:
Yes, it's in one of the headers.

Um, I asked "where exactly".
I think the idea is: it gives flexibility to change between float (=small
size, low precision) or long double (=high size, high precision) or
double (=compromise) at a later stage, without needing to change all
definitions everywhere in code : only the typeDef needs changing.

That's a fine idea -- but FLOAT is a lousy name for it.
 
M

Malcolm McLean

בת×ריך ×™×•× ×©× ×™,4 ביוני 2012 20:20:43 UTC+1, מ×ת Keith Thompson:
That's a fine idea -- but FLOAT is a lousy name for it.
REAL would be better.
However not all programmers know what that means.
 
J

James Kuyper

בת×ריך ×™×•× ×©× ×™, 4 ביוני 2012 20:20:43 UTC+1, מ×ת Keith Thompson:
REAL would be better.
However not all programmers know what that means.

A typedef like that is always intended for use in a specific context; it
should not mean "the floating point type that should be used in all
contexts". It should mean something like "the floating point type to be
used when working with this particular library", and the name should
contain at least a hint of the correct context; neither "FLOAT" nor
"REAL" does so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,207
Latest member
HelenaCani

Latest Threads

Top