xref: /openbsd-src/regress/lib/libm/toint/toint.c (revision ddd20476a41851e342359422857807dd352a1086)
1 /*	$OpenBSD: toint.c,v 1.7 2010/02/06 18:53:19 otto Exp $	*/
2 
3 /*	Written by Michael Shalayeff, 2003, Public domain.	*/
4 
5 #include <limits.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <signal.h>
10 #include <unistd.h>
11 
12 void
sigfpe(int sig,siginfo_t * si,void * v)13 sigfpe(int sig, siginfo_t *si, void *v)
14 {
15 	char buf[132];
16 
17 	if (si) {
18 		snprintf(buf, sizeof(buf), "sigfpe: trap=%d code=%d addr=%p\n",
19 		    si->si_trapno, si->si_code, si->si_addr);
20 		write(1, buf, strlen(buf));
21 	}
22 	_exit(1);
23 }
24 
25 int
toint(double d)26 toint(double d)
27 {
28 	return (int)(d + 1);
29 }
30 
31 long long
toll(double d)32 toll(double d)
33 {
34 	return (long long)d;
35 }
36 
37 int
main(int argc,char * argv[])38 main(int argc, char *argv[])
39 {
40 	struct sigaction sa;
41 	int i;
42 
43 	memset(&sa, 0, sizeof(sa));
44 	sa.sa_sigaction = sigfpe;
45 	sa.sa_flags = SA_SIGINFO;
46 	sigaction(SIGFPE, &sa, NULL);
47 
48 	if (toint(8.6) != 9)
49 		exit(1);
50 
51 	if (toll(2.3456e+17) != 234560000000000000LL)
52 		exit(1);
53 
54 	exit(0);
55 }
56