🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.os.linux.mandrake
Article #520

Re: Problems with float variables in C

#520
From: Dann Corbit
Date: Sat, 08 Sep 2001 05:42
97 lines
4239 bytes
"Edward Rosten" <u98ejr@ecs.ox.ac.uk> wrote in message
news:clcm-20010907-0010@plethora.net...
> > 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?


Depends upon whether or not the compiler has denormal numbers.

C:\tmp>type foo.c
#include <stdio.h>
#include <float.h>
int             main(void)
{
    float           fm = FLT_MIN;
    double          dm = DBL_MIN;

    printf("FLT_MIN     =%g\n", fm);
    printf("FLT_MIN*0.5f=%g\n", fm * 0.5f);

    printf("DBL_MIN     =%g\n", dm);
    printf("DBL_MIN*0.5f=%g\n", dm * 0.5);
    return 0;
}

C:\tmp>cl /W4 /Ox foo.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.

foo.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/out:foo.exe
foo.obj

C:\tmp>foo
FLT_MIN     =1.17549e-038
FLT_MIN*0.5f=5.87747e-039
DBL_MIN     =2.22507e-308
DBL_MIN*0.5f=1.11254e-308

C:\tmp>icl /W5 /Ox foo.c
Intel(R) C++ Compiler for 32-bit applications, Version 5.0.1   Build 010525Z
Copyright (C) 1985-2001 Intel Corporation.  All rights reserved.

foo.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

-out:foo.exe
foo.obj

C:\tmp>foo
FLT_MIN     =1.17549e-038
FLT_MIN*0.5f=5.87747e-039
DBL_MIN     =2.22507e-308
DBL_MIN*0.5f=1.11254e-308

$ gcc -O3 -W -Wall -ansi -pedantic foo.c -ofoo.exe

dcorbit@DANNFAST c:/tmp
$ ./foo
FLT_MIN     =1.17549e-38
FLT_MIN*0.5f=5.87747e-39
DBL_MIN     =2.22507e-308
DBL_MIN*0.5f=1.11254e-308

Of course, I'm not terribly sure how the test helps, since
FLT_MAX/FLT_MIN would trigger an FPE, if floating point exceptions were
available and enabled for overflow.
--
C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 "The C-FAQ Book" ISBN 0-201-84519-9
C.A.P. FAQ: ftp://cap.connx.com/pub/Chess%20Analysis%20Project%20FAQ.htm
-- 
comp.lang.c.moderated - moderation address: clcm@plethora.net
Technical Details
Message-ID: <clcm-20010908-0002@plethora.net>
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp.abs.net!news-out.visi.com!hermes.visi.com!gemini.plethora.net.POSTED!not-for-mail
References: <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-07 22:42:13 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!newsfeed.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp.abs.net!news-out.visi.com!hermes.visi.com!gemini.plethora.net.POSTED!not-for-mail
Approved: clc@plethora.net
Content-Transfer-encoding: 7bit
References: <clcm-20010905-0003@plethora.net> <clcm-20010905-0016@plethora.net> <clcm-20010907-0010@plethora.net>
X-Priority: 3
Content-Type: text/plain; charset="iso-8859-1"
X-Mimeole: Produced By Microsoft MimeOLE V5.50.4522.1200
Subject: Re: Problems with float variables in C
From: "Dann Corbit" <dcorbit@connx.com>
Organization: http://extra.newsguy.com
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-Msmail-priority: Normal
Return-Path: <news@lani.pathlink.com>
Mime-Version: 1.0
Newsgroups: alt.os.linux.mandrake,comp.lang.c.moderated,comp.programming,gnu.gcc,gnu.gcc.help
Message-ID: <clcm-20010908-0002@plethora.net>
Originator: clcm@plethora.net (Comp Lang C'Moderated)
Date: 08 Sep 2001 05:42:12 GMT
Lines: 97
NNTP-Posting-Host: 24803d54.news.plethora.net
X-Trace: 999927732 gemini.plethora.net 328 clcm@205.166.146.1
X-Complaints-To: abuse@plethora.net