Article View: alt.os.linux.mandrake
Article #506Re: Problems with float variables in C
From: Horst Kraemer
Date: Sat, 08 Sep 2001 19:10
Date: Sat, 08 Sep 2001 19:10
65 lines
3378 bytes
3378 bytes
On 08 Sep 2001 00:22:14 GMT, u98ejr@ecs.ox.ac.uk (Edward Rosten)
wrote:
> > When checking for possible difficulty with division by zero, if you don't
> > wish to get into trapping overflow and the like, you should check at least
> > for small non-zero numbers. If all your numerators have absolute value < 4,
> > you could safely do
> > #include <float.h>
> > if(fabs(y)>FLT_MIN)
> > proceed with division by y
> > else
> > treat as error
>
> I'm not trying to be pedantic, I'm genuinely curious, but isn't
>
> fabs(y) > FLT_MIN
>
> equivalent to
>
> f != 0
>
> Since 0 can be represented exactly and FLT_MIN is the smallest
> possible float greater than zero?
Usually not. Lets take a bread and butter i87 implementation.
According to the C Standard FLT_MIN denotes the smallest _normalized_
float value. Normalized means "full precision", in this case 24
significant mantissa bits evaluating to some
value = 1.xxx xxxx xxxx xxxx xxxx xxxx * 2^n
whiere n is in the range between -126 and +127
In IEEE SINGLE which is usally mapped to a C float FLT_MIN corresponds
to
1.000 0000 0000 0000 0000 0000 * 2^-126 = 2^-126
This isn't the smallest possible float value >0 because there are
still float values of the form
value = 0.xxx xxxx xxxx xxxx xxxx xxxx * 2^-126
down to
realmin = 0.000 0000 0000 0000 0000 0001 * 2^-126 = 2^-149
which is the "real" denormal float value with one single bit of
precision. All i87-based float implementation I know support
denormals, i.e. values greater equal 2^-149 and less than 2^-126 =
FLT_MIN as if they were normals.
You may test this on your imlementation
float f;
for (f=FLT_MIN;f;f/=2) printf("%f\n",f);
after compiling it with _no_ optimizations.
Regards
Horst
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
Technical Details
Message-ID:
<clcm-20010908-0004@plethora.net>Path:
arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-out.visi.com!hermes.visi.com!gemini.plethora.net.POSTED!not-for-mailReferences:
<clcm-20010905-0003@plethora.net> <clcm-20010905-0016@plethora.net> <clcm-20010907-0010@plethora.net>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11318c,898a7d80327816b3 X-Google-Attributes: gid11318c,public X-Google-Thread: f65d0,898a7d80327816b3 X-Google-Attributes: gidf65d0,public X-Google-Thread: f8a83,898a7d80327816b3 X-Google-Attributes: gidf8a83,public X-Google-Thread: 100850,77ebfbc06b0f9856 X-Google-Attributes: gid100850,public X-Google-Thread: 108717,898a7d80327816b3 X-Google-Attributes: gid108717,public X-Google-ArrivalTime: 2001-09-08 12:10:41 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news-out.visi.com!hermes.visi.com!gemini.plethora.net.POSTED!not-for-mail Subject: Re: Problems with float variables in C Approved: clc@plethora.net References: <clcm-20010905-0003@plethora.net> <clcm-20010905-0016@plethora.net> <clcm-20010907-0010@plethora.net> From: hhkraemer@web.de (Horst Kraemer) X-Newsreader: Forte Free Agent 1.21/32.243 X-Orig-x-trace: fu-berlin.de 999943280 6809321 213.7.60.144 (16 [27606]) Return-Path: <hhkraemer@web.de> Newsgroups: alt.os.linux.mandrake,comp.lang.c.moderated,comp.programming,gnu.gcc,gnu.gcc.help Message-ID: <clcm-20010908-0004@plethora.net> X-Orig-nntp-posting-host: b3c90.pppool.de (213.7.60.144) Originator: clcm@plethora.net (Comp Lang C'Moderated) Date: 08 Sep 2001 19:10:31 GMT Lines: 65 NNTP-Posting-Host: f6c03a00.news.plethora.net X-Trace: 999976231 gemini.plethora.net 332 clcm@205.166.146.1 X-Complaints-To: abuse@plethora.net