* "Arithmetic exception" with modulus operator '%' @ 2019-02-15 3:35 jounijl 2019-02-14 23:58 ` Rich Felker 2019-02-15 10:05 ` Szabolcs Nagy 0 siblings, 2 replies; 5+ messages in thread From: jounijl @ 2019-02-15 3:35 UTC (permalink / raw) To: musl As in the headline. Program stops and prints "Arithmetic exception" at the line where the modulus operator '%' is. I'm compiling in Alpine linux with clang installed from apk:s: clang -c test.c clang -o test test.o The code is: ----- snip ----- unsigned int unum = 0; unsigned int umod = 0; unsigned int ures = 0; ures = unum % umod; // <-- this one ----- /snip ----- The variables have some values other than 0. The environment is: The Alpine Linux is installed in Oracle Virtualbox in FreeBSD 12, 64-bit Intel. $ uname -a Linux localhost 4.14.89-0-vanilla #1-Alpine SMP Tue Dec 18 16:10:10 UTC 2018 x86_64 GNU/Linux $ clang --version Alpine clang version 5.0.1 (tags/RELEASE_501/final) (based on LLVM 5.0.1) What does this? Do I need to include some library? w.b.reg., Jouni ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: "Arithmetic exception" with modulus operator '%' 2019-02-15 3:35 "Arithmetic exception" with modulus operator '%' jounijl @ 2019-02-14 23:58 ` Rich Felker 2019-02-15 10:31 ` jounijl 2019-02-15 10:05 ` Szabolcs Nagy 1 sibling, 1 reply; 5+ messages in thread From: Rich Felker @ 2019-02-14 23:58 UTC (permalink / raw) To: jounijl; +Cc: musl On Fri, Feb 15, 2019 at 03:35:23AM +0000, jounijl@yahoo.co.uk wrote: > > As in the headline. Program stops and prints "Arithmetic exception" > at the line where the modulus operator '%' is. > > I'm compiling in Alpine linux with clang installed from apk:s: > > clang -c test.c > clang -o test test.o > > The code is: > > ----- snip ----- > unsigned int unum = 0; > unsigned int umod = 0; > unsigned int ures = 0; > ures = unum % umod; // <-- this one > ----- /snip ----- > > The variables have some values other than 0. I don't follow. You say they have some value other than 0, but the above example snippet has them as zero, and if they're 0 it's undefined behavior and a fault of some sort is a typical result. What did you expect to happen? Rich ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: "Arithmetic exception" with modulus operator '%' 2019-02-14 23:58 ` Rich Felker @ 2019-02-15 10:31 ` jounijl 2019-02-15 17:21 ` Rich Felker 0 siblings, 1 reply; 5+ messages in thread From: jounijl @ 2019-02-15 10:31 UTC (permalink / raw) To: Rich Felker; +Cc: musl Exactly. To be complite: The host machine prints: "Floating point exception" and outputs a core file. Uses: /lib/libc.so.7 The Alpine prints: "Arithmetic exception". Uses: /lib/ld-musl-x86_64.so.1 Solaris 10 prints: "Arithmetic exception". Uses: /lib/libc.so.1 ; /lib/libm.so.2 Ubuntu prints: "Floating point exception" and outputs a core file. Uses: /lib/x86_64-linux-gnu/libc.so.6 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 ? Maby the core file has to be enabled somehow. Attached is a complite test program. The word "trap" is unfamiliar to me. I think it means just checking with an 'if'-like comparison. Thank you for the replies. It helped. The simple facts are not always obvious. Jouni the file: $ cat m.c #include <stdio.h> int main( int argc, char *argv[] ); int main( int argc, char *argv[] ){ unsigned int z = 0; unsigned int w = 0; //floating point exception: fprintf( stderr, "\n 0 %% 0 = %i", (int) z%w ); z = 1; //floating point exception: fprintf( stderr, "\n 1 %% 0 = %i", (int) z%w ); z = 0; w = 1; fprintf( stderr, "\n 0 %% 1 = %i", (int) z%w ); z = 1; w = 1; fprintf( stderr, "\n 1 %% 1 = %i", (int) z%w ); } On 14.2.2019 23.58, Rich Felker wrote: > On Fri, Feb 15, 2019 at 03:35:23AM +0000, jounijl@yahoo.co.uk wrote: >> As in the headline. Program stops and prints "Arithmetic exception" >> at the line where the modulus operator '%' is. >> >> I'm compiling in Alpine linux with clang installed from apk:s: >> >> clang -c test.c >> clang -o test test.o >> >> The code is: >> >> ----- snip ----- >> unsigned int unum = 0; >> unsigned int umod = 0; >> unsigned int ures = 0; >> ures = unum % umod; // <-- this one >> ----- /snip ----- >> >> The variables have some values other than 0. > I don't follow. You say they have some value other than 0, but the > above example snippet has them as zero, and if they're 0 it's > undefined behavior and a fault of some sort is a typical result. What > did you expect to happen? > > Rich ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: "Arithmetic exception" with modulus operator '%' 2019-02-15 10:31 ` jounijl @ 2019-02-15 17:21 ` Rich Felker 0 siblings, 0 replies; 5+ messages in thread From: Rich Felker @ 2019-02-15 17:21 UTC (permalink / raw) To: jounijl; +Cc: musl On Fri, Feb 15, 2019 at 10:31:22AM +0000, jounijl@yahoo.co.uk wrote: > > Exactly. To be complite: > > The host machine prints: "Floating point exception" and outputs a > core file. Uses: /lib/libc.so.7 > The Alpine prints: "Arithmetic exception". Uses: /lib/ld-musl-x86_64.so.1 > Solaris 10 prints: "Arithmetic exception". Uses: /lib/libc.so.1 ; > /lib/libm.so.2 > Ubuntu prints: "Floating point exception" and outputs a core file. > Uses: /lib/x86_64-linux-gnu/libc.so.6 > > 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 instead: #define MY_MOD(a,b) (!(b) ? (a) : (b)==-1 ? 0 : (a)%(b)) Rich ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: "Arithmetic exception" with modulus operator '%' 2019-02-15 3:35 "Arithmetic exception" with modulus operator '%' jounijl 2019-02-14 23:58 ` Rich Felker @ 2019-02-15 10:05 ` Szabolcs Nagy 1 sibling, 0 replies; 5+ messages in thread From: Szabolcs Nagy @ 2019-02-15 10:05 UTC (permalink / raw) To: musl; +Cc: jounijl * jounijl@yahoo.co.uk <jounijl@yahoo.co.uk> [2019-02-15 03:35:23 +0000]: > As in the headline. Program stops and prints "Arithmetic exception" at the > line where the modulus operator '%' is. > > I'm compiling in Alpine linux with clang installed from apk:s: > > clang -c test.c > clang -o test test.o > > The code is: > > ----- snip ----- > unsigned int unum = 0; > unsigned int umod = 0; > unsigned int ures = 0; > ures = unum % umod; // <-- this one undefined behaviour == anything can happen the compiler can drop this entirely. > ----- /snip ----- > > The variables have some values other than 0. > > The environment is: > The Alpine Linux is installed in Oracle Virtualbox in FreeBSD 12, 64-bit > Intel. x86_64 traps mod by 0 (in case the compiler didn't drop the code path because of ub). > > $ uname -a > Linux localhost 4.14.89-0-vanilla #1-Alpine SMP Tue Dec 18 16:10:10 UTC 2018 > x86_64 GNU/Linux > > $ clang --version > Alpine clang version 5.0.1 (tags/RELEASE_501/final) (based on LLVM 5.0.1) > > What does this? Do I need to include some library? what behaviour do you expect? > > w.b.reg., Jouni ^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2019-02-15 17:21 UTC | newest] Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2019-02-15 3:35 "Arithmetic exception" with modulus operator '%' jounijl 2019-02-14 23:58 ` Rich Felker 2019-02-15 10:31 ` jounijl 2019-02-15 17:21 ` Rich Felker 2019-02-15 10:05 ` Szabolcs Nagy
Code repositories for project(s) associated with this public inbox https://git.vuxu.org/mirror/musl/ 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).