Article View: alt.os.linux.mandrake
Article #508Re: Problems with float variables in C
From: John Simmons
Date: Mon, 10 Sep 2001 03:08
Date: Mon, 10 Sep 2001 03:08
60 lines
3374 bytes
3374 bytes
In article <clcm-20010907-0010@plethora.net>, u98ejr@ecs.ox.ac.uk
says...
> > 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?
>
Even better, why treat it as an error when you can gracefully recover by
simply making y = 1.0 instead. I would also use doubles instead of
floats, as they allow you to represent much larger numbers.
Without knowing the context of the original code, nore being able to see
the original question, I would do this:
if (y <= 0.0)
{
y = 1.0;
}
// proceed with division by y
This way, the division will ALWAYS be done, and will ALWAYS work.
--
=========================================================
Redneck Techno-Biker & "programming deity"
http://www.paddedwall.org/john
DeMONS/1 for Nascar Racing 3 & Nascar Legends
http://www.paddedwall.org/demons
DeMONS/2 for Nascar Racing 4 (in development)
http://www.paddedwall.org/demons2
Barbarian Diecast Collector (460+ cars and counting)
http://www.paddedwall.org/diecast
If you want to send me email, go to the first URL shown
above & click "Send Me Mail" in the contents frame.
=========================================================
--
comp.lang.c.moderated - moderation address: clcm@plethora.net
Technical Details
Message-ID:
<clcm-20010909-0006@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: 108717,898a7d80327816b3 X-Google-Attributes: gid108717,public X-Google-Thread: f8a83,898a7d80327816b3 X-Google-Attributes: gidf8a83,public X-Google-Thread: f65d0,898a7d80327816b3 X-Google-Attributes: gidf65d0,public X-Google-Thread: 100850,77ebfbc06b0f9856 X-Google-Attributes: gid100850,public X-Google-ArrivalTime: 2001-09-09 20:08:59 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 X-Old-trace: typhoon.austin.rr.com 1000041114 24.162.171.217 (Sun, 09 Sep 2001 08:11:54 CDT) 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: John Simmons <chaos@panic.com> Organization: Road Runner - Texas X-Newsreader: MicroPlanet Gravity v2.30 Return-Path: <news@newsr2.texas.rr.com> X-Old-complaints-to: abuse@rr.com Newsgroups: alt.os.linux.mandrake,comp.lang.c.moderated,comp.programming,gnu.gcc,gnu.gcc.help Message-ID: <clcm-20010909-0006@plethora.net> Originator: clcm@plethora.net (Comp Lang C'Moderated) Date: 10 Sep 2001 03:08:56 GMT Lines: 60 NNTP-Posting-Host: fa2e85e3.news.plethora.net X-Trace: 1000091336 gemini.plethora.net 318 clcm@205.166.146.1 X-Complaints-To: abuse@plethora.net