-
Notifications
You must be signed in to change notification settings - Fork 1
/
k.c
81 lines (75 loc) · 1.93 KB
/
k.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "k.h"
#include <stdio.h>
#include <limits.h>
#include <math.h>
#include <string.h>
#include "x.h"
#include "scope.h"
void* px(U x){return(void*)k(0,x,0);}
void kfree(U x){if(ax)return; k(15,0,x);}
U kref(U x){if(ax)return x; return k(0,0,x);}
void kinit() {
scope_init();
}
static void pi(int i, char *c) {
if(i==INT_MAX) printf("0I%s",c);
else if(i==INT_MIN) printf("0N%s",c);
else if(i==INT_MIN+1) printf("-0I%s",c);
else printf("%d%s",i,c);
}
static void pf(float f, char *c) {
char ds[256];
if(isinf(f)&&f>0.0) printf("0i%s",c);
else if(isnan(f)) printf("0n%s",c);
else if(isinf(f)&&f<0.0) printf("-0i%s",c);
else {
sprintf(ds,"%0.*g",7,f);
if(!strchr(ds,'.')&&!strchr(ds,'e')) strcat(ds, ".0");
printf("%s%s",ds,c);
}
}
static int ffix(char *ds, int aaa) {
if(strchr(ds,'.')||strchr(ds,'e')) return 0;
if(!strcmp(ds,"inf")) { sprintf(ds,"0i"); return 0; }
if(!strcmp(ds,"nan")) { sprintf(ds,"0n"); return 0; }
if(!strcmp(ds,"-inf")) { sprintf(ds,"-0i"); return 0; }
return aaa;
}
void kprint(U x) {
int *a,h,j,aaa=1;
char ds[256];
if(ax) {
if(tx==3) pi((int)x,"\n");
else if(tx==4) { h=(int)x; pf(*(float*)&h,"\n"); }
}
else { /* vector */
//a=px(x);
a=(int*)k(0,x,0);
if(tx==3) {
if(!nx) printf("!0\n");
else if(1==nx) { printf(","); pi(a[0],"\n"); }
else {
for(j=0;j<nx-1;j++) pi(a[j]," ");
pi(a[j],"\n");
}
}
else if(tx==4) {
if(!nx) printf("0#0.0\n");
else if(1==nx) { printf(","); pf(*(float*)&a[0],"\n"); }
else {
sprintf(ds,"%0.*g",7,*(float*)&a[0]);
aaa=ffix(ds,aaa);
printf("%s ",ds);
for(j=1;j<nx-1;j++) {
sprintf(ds,"%0.*g",7,*(float*)&a[j]);
aaa=ffix(ds,aaa);
printf("%s ",ds);
}
sprintf(ds,"%0.*g",7,*(float*)&a[j]);
aaa=ffix(ds,aaa);
if(aaa) strcat(ds,".0");
printf("%s\n",ds);
}
}
}
}