Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Date: Fri, 15 Feb 2019 12:21:15 -0500
From: Rich Felker <>
To: "" <>
Subject: Re: "Arithmetic exception" with modulus operator '%'

On Fri, Feb 15, 2019 at 10:31:22AM +0000, wrote:
> Exactly. To be complite:
> The host machine prints: "Floating point exception" and outputs a
> core file. Uses: /lib/
> The Alpine prints: "Arithmetic exception".  Uses: /lib/
> Solaris 10 prints: "Arithmetic exception". Uses: /lib/ ;
> /lib/
> Ubuntu prints: "Floating point exception" and outputs a core file.
> Uses: /lib/x86_64-linux-gnu/
> To the question "what do you except":
> Of course the behaviour is similar to others and this is correct. As
> in programs the behaviour would be best like this: number%zero would
> be the number it self when number/zero is undefined or infinity
> (maby set the number to the largest known number). To change this,
> some mathematical evaluation would be needed. Answer: mod 0:
> Convenient would be the number it self ?

This has nothing to do with musl or library implementation; what
you're asking for is a *compiler* that defines certain undefined
behavior in a particular way. Even if you had such a thing, writing C
code in order to depend on nonstandard behavior of a particular
compiler would not be a reasonable thing to do.

A better way to achieve the same thing would be just writing a
function that does what you want:

int my_mod(int a, int b)
	if (!b) return a;
	else if (b==-1) return 0;
	else return a%b;

and using that instead of using the % operator directly. If you need
it to work in constant expression contexts, you could use a macro

#define MY_MOD(a,b) (!(b) ? (a) : (b)==-1 ? 0 : (a)%(b))


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.