9fans - fans of the OS Plan 9 from Bell Labs
 help / color / mirror / Atom feed
* [9fans] c99 div and mod round toward zero
@ 2006-04-16  1:53 erik quanstrom
  2006-04-16  3:15 ` Russ Cox
  2006-04-16  3:56 ` geoff
  0 siblings, 2 replies; 5+ messages in thread
From: erik quanstrom @ 2006-04-16  1:53 UTC (permalink / raw)
  To: 9fans

according to /sys/src/cmd/cc/c99:

	Not done (yet?):
	[...]
	25. Division and mod truncate toward zero.

on 8c this works as expected. i.e.

	; cat mod.c
	#include <u.h>
	#include <libc.h>

	void
	main(void)
	{
		fprint(2, "-22/7 = %d\n", -22/7);
		fprint(2, "-22%%7 = %d\n", -22%7);
	}
	; 8c mod.c && 8l mod.8 && 8.out
	-22  7 = -3
	-22 % 7 = -1

is this fixed, or an archiitechture-dependent thing?  i don't
have a way of checking anything else.

- erik


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] c99 div and mod round toward zero
  2006-04-16  1:53 [9fans] c99 div and mod round toward zero erik quanstrom
@ 2006-04-16  3:15 ` Russ Cox
  2006-04-16  3:56 ` geoff
  1 sibling, 0 replies; 5+ messages in thread
From: Russ Cox @ 2006-04-16  3:15 UTC (permalink / raw)
  To: 9fans

> is this fixed, or an archiitechture-dependent thing?  i don't
> have a way of checking anything else.

traditionally it is architecture+compiler dependent.
off the top of my head i don't know what the other
compilers do.

russ



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] c99 div and mod round toward zero
  2006-04-16  1:53 [9fans] c99 div and mod round toward zero erik quanstrom
  2006-04-16  3:15 ` Russ Cox
@ 2006-04-16  3:56 ` geoff
  2006-04-16  5:33   ` Russ Cox
  1 sibling, 1 reply; 5+ messages in thread
From: geoff @ 2006-04-16  3:56 UTC (permalink / raw)
  To: 9fans

I took Erik's question to be whether 8c rounded to zero because the
x86 (or 8c) does it that way or because the common C compiler code had
been changed to force C99 behaviour.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] c99 div and mod round toward zero
  2006-04-16  3:56 ` geoff
@ 2006-04-16  5:33   ` Russ Cox
  0 siblings, 0 replies; 5+ messages in thread
From: Russ Cox @ 2006-04-16  5:33 UTC (permalink / raw)
  To: 9fans

> I took Erik's question to be whether 8c rounded to zero because the
> x86 (or 8c) does it that way or because the common C compiler code had
> been changed to force C99 behaviour.

the former.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [9fans] c99 div and mod round toward zero
@ 2006-04-16  3:48 dmr
  0 siblings, 0 replies; 5+ messages in thread
From: dmr @ 2006-04-16  3:48 UTC (permalink / raw)
  To: 9fans

 > is this [rounding to 0] fixed, or an archiitechture-dependent thing?  i don't
 > have a way of checking anything else.

In C89 it was allowed to vary by architecture (subject to obvious consistency
constraints).  In C99 the result is well-determined, and does round to 0.
This is consistent with the usual math convention, but
in fact not necessarily best (e.g. for graphics, as Rob has pointed out).

	Dennis


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2006-04-16  5:33 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-16  1:53 [9fans] c99 div and mod round toward zero erik quanstrom
2006-04-16  3:15 ` Russ Cox
2006-04-16  3:56 ` geoff
2006-04-16  5:33   ` Russ Cox
2006-04-16  3:48 dmr

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).