develooper Front page | perl.perl5.porters | Postings from January 2012

Re: [perl #107702] Rounding error in sprintf.

Thread Next
Salvatore Domenick Desiano
January 6, 2012 18:13
Re: [perl #107702] Rounding error in sprintf.
Message ID:
Sam's answer indicates that this is is a result of an efficient
implementation within Perl, which I would argue, using the principle of
least surprise, is a bug if it gives incorrect answers for math that could
be handled within an int. Perhaps a pragma forcing the local implementation
to be used would be a reasonable compromise.

I'm not sure Graham's answer covers it -- program below prints "247", while
perl prints "246", so sprintf isn't behaving the same as the underlying C.

-- Sal

#include <stdio.h>

int main(void)
  float a = 2.47;
  char buf[100];
  sprintf(buf, "%d", int(a * 100));
  printf("%s\n", buf);
  return 0;

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About