delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/08/05/09:45:24

From: dontmailme AT iname DOT com (Steamer)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: modf BROKEN ??
Date: Tue, 01 Aug 2000 18:43:13 GMT
Organization: always disorganized
Lines: 26
Message-ID: <39871a3d.38832761@news.freeserve.net>
References: <crdcosk167oh81vpnjfqeg1bnls5qqg9ld AT 4ax DOT com> <f8udosk8samrov7j0156qkuk31aga0essa AT 4ax DOT com>
NNTP-Posting-Host: modem-184.blue-streak-damsel.dialup.pol.co.uk
X-Trace: newsg2.svr.pol.co.uk 965155394 25666 62.136.241.184 (1 Aug 2000 18:43:14 GMT)
NNTP-Posting-Date: 1 Aug 2000 18:43:14 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Newsreader: Forte Free Agent 1.11/32.235
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Radical NetSurfer wrote:

> At 5.0, 6.0, etc, Fract display as "1.0" because of a rounding error
> in the   printf   output.  Its only an "optical illusion", and if 
> %3.21 is used, you won't see it.
> 
> the question is obvious:
> HOW do I maintain a %3.3f  output, but NOT get the 
> 1.0 "optical illusion"  ???

This is not an obvious question at all, especially as you didn't
use %3.3f in your previous post.

One way to print out the fractional part without this rounding
problem is to use sprintf to print the entire number to a buffer,
and then print out the fractional part from there.  Or (assuming
your numbers are positive and not too big) you could do this sort
of thing:

    ((unsigned long)(1000.0*f+0.5)) % 1000

and print that out with leading zeroes.  In fact, depending on
what you're doing, it may be better not to use floating-point
numbers at all - just use suitably scaled integers.

S.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019