Skip to content

Commit 30f6db1

Browse files
committed
Merge branch 'jfkdev' into 'bkedev'
Draft merge trig into others See merge request rwp/rwloadsim!2
2 parents 41a2e07 + 3fd5cc4 commit 30f6db1

File tree

8 files changed

+146
-17
lines changed

8 files changed

+146
-17
lines changed

man/man1rwl/expression.1rwl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ If p is omitted, return e raised to the power of a.
315315
.B log(l,a) log(a)
316316
Return the logarithm of base l, respectively e, of the argument a.
317317
.TP
318+
.B sin(a) cos(a)
319+
Return the sine or cosine of argument a degrees.
320+
.TP
321+
.B atan2(b,a)
322+
Return the angle in degrees between the a-coordinate and b-coordinate for the quadrant.
323+
.TP
318324
.B lengthb(s)
319325
Return the length in bytes of the string s.
320326
.TP

src/rwl.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
*
1414
* History
1515
*
16-
* bengsig 9-nov-2023 - Increase RWL_MAX_VAR
16+
* johnkenn 02-nov-2023 - trignometry sin, cos, atan2
17+
* bengsig 9-nov-2023 - Increase RWL_MAX_VAR
18+
* johnkenn 02-nov-2023 - trignometry sin, cos, atan2
1719
* bengsig 3-oct-2023 - Now development for 3.1.1
1820
* bengsig 2-oct-2023 - Releasing 3.1.0
1921
* bengsig 22-sep-2023 - ampersand fixes
@@ -1026,6 +1028,7 @@ struct rwl_main
10261028
#define RWL_P4_SQLLOGFILE 0x00000008 // $sqllogging to real file that we must close
10271029
#define RWL_P4_AMPERSAND 0x00000010 // ampersand replacement in embedded sql is on
10281030
#define RWL_P4_URLERRORON 0x00000020 // do not turn of error URL
1031+
#define RWL_P4_TRIGRAD 0x00000040 // Make triginometry functions use gradients
10291032
FILE *sqllogfile;
10301033

10311034
int userexit; // value for user exit
@@ -1355,6 +1358,9 @@ enum rwl_stack_t
13551358
, RWL_STACK_RUNSECONDS /* runseconds function */
13561359
, RWL_STACK_EPOCHSECONDS /* epochseconds function */
13571360
, RWL_STACK_SQRT /* sqrt() function */
1361+
, RWL_STACK_SIN /* sin() function */
1362+
, RWL_STACK_COS /* cos() function */
1363+
, RWL_STACK_ATAN2 /* atan2(y,x) function */
13581364
, RWL_STACK_LENGTHB /* lengthb() function */
13591365
, RWL_STACK_SUBSTRB2 /* substrb(s,p) function */
13601366
, RWL_STACK_SUBSTRB3 /* substrb(s,p,l) function */

src/rwlexprcomp.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*
2020
* History
2121
*
22+
* johnkenn 02-nov-2023 - Add sin and cos
2223
* bengsig 19-jul-2023 - Fix constants rwl_null etc.
2324
* bengsig 17-jul-2023 - % works on double
2425
* bengsig 10-jul-2023 - ceil, trunc, floor functions
@@ -804,6 +805,7 @@ rwl_estack *rwlexprfinish(rwl_main *rwm)
804805
case RWL_STACK_NORMALRANDOM:
805806
case RWL_STACK_ERLANGK:
806807
case RWL_STACK_EXPB:
808+
case RWL_STACK_ATAN2:
807809
case RWL_STACK_LOGB:
808810
rwlasrti(2,"logb");
809811
estk[i].evaltype = tstk[i] = RWL_TYPE_DBL;
@@ -961,6 +963,8 @@ rwl_estack *rwlexprfinish(rwl_main *rwm)
961963
case RWL_STACK_FLOOR:
962964
case RWL_STACK_ROUND:
963965
case RWL_STACK_SQRT:
966+
case RWL_STACK_SIN:
967+
case RWL_STACK_COS:
964968
case RWL_STACK_ERLANG:
965969
case RWL_STACK_ERLANG2:
966970
rwlasrti(1,"doublfunc");

src/rwlexpreval.c

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*
1515
* History
1616
*
17+
* johnkenn 06-nov-2023 - trigonometry sin, cos, atan2
1718
* bengsig 25-sep-2023 - fix if doublevar then
1819
* bengsig 19-jul-2023 - assign empty or only space to int/dbl is NULL
1920
* bengsig 17-jul-2023 - % works on double
@@ -234,6 +235,9 @@ void rwlexpreval ( rwl_estack *stk , rwl_location *loc , rwl_xeqenv *xev , rwl_v
234235
case RWL_STACK_FLOOR:
235236
case RWL_STACK_CEIL:
236237
case RWL_STACK_TRUNC:
238+
case RWL_STACK_SIN:
239+
case RWL_STACK_COS:
240+
case RWL_STACK_ATAN2:
237241
break;
238242

239243
case RWL_STACK_END:
@@ -460,6 +464,18 @@ void rwlexpreval ( rwl_estack *stk , rwl_location *loc , rwl_xeqenv *xev , rwl_v
460464
fprintf(stderr," SQRT");
461465
break;
462466

467+
case RWL_STACK_SIN:
468+
fprintf(stderr," SIN");
469+
break;
470+
471+
case RWL_STACK_COS:
472+
fprintf(stderr," COS");
473+
break;
474+
475+
case RWL_STACK_ATAN2:
476+
fprintf(stderr, "ATAN2");
477+
break;
478+
463479
case RWL_STACK_ERLANG:
464480
fprintf(stderr," ERLANG");
465481
break;
@@ -2690,6 +2706,25 @@ void rwlexpreval ( rwl_estack *stk , rwl_location *loc , rwl_xeqenv *xev , rwl_v
26902706
goto finish_two_math;
26912707
break;
26922708

2709+
case RWL_STACK_ATAN2:
2710+
if (i<2) goto stack2short;
2711+
if (tainted || skip) goto pop_two;
2712+
if (bit(xev->rwm->m4flags, RWL_P4_TRIGRAD))
2713+
{
2714+
resdval = atan2(cstak[i-2].dval, cstak[i-1].dval);
2715+
}
2716+
else
2717+
{
2718+
resdval = (atan2(cstak[i-2].dval, cstak[i-1].dval) * (180.0 / M_PI));
2719+
}
2720+
resival = (sb8)round(resdval);
2721+
if (bit(xev->tflags,RWL_THR_DEVAL))
2722+
rwldebugcode(xev->rwm, loc, "at %d: atan2(%.2f,%.2f)=%.2f", i, cstak[i-2].dval
2723+
, cstak[i-1].dval,resdval);
2724+
rtyp = RWL_TYPE_DBL;
2725+
goto finish_two_math;
2726+
break;
2727+
26932728
case RWL_STACK_CEIL:
26942729
if (i<1) goto stack1short;
26952730
if (tainted || skip) goto pop_one;
@@ -2750,6 +2785,44 @@ void rwlexpreval ( rwl_estack *stk , rwl_location *loc , rwl_xeqenv *xev , rwl_v
27502785
goto finish_one_math;
27512786
break;
27522787

2788+
case RWL_STACK_SIN:
2789+
if (i<1) goto stack1short;
2790+
if (tainted || skip) goto pop_one;
2791+
if (bit(xev->rwm->m4flags, RWL_P4_TRIGRAD))
2792+
{
2793+
resdval = sin(cstak[i-1].dval);
2794+
}
2795+
else
2796+
{
2797+
resdval = sin(cstak[i-1].dval) * (M_PI / 180.0);
2798+
}
2799+
resival = (sb8)round(resdval);
2800+
if (bit(xev->tflags,RWL_THR_DEVAL))
2801+
rwldebugcode(xev->rwm, loc, "at %d: sin(%.2f) = %.2f", i, cstak[i-1].dval, resdval);
2802+
rtyp = RWL_TYPE_DBL;
2803+
goto finish_one_math;
2804+
break;
2805+
2806+
case RWL_STACK_COS:
2807+
if (i<1) goto stack1short;
2808+
if (tainted || skip) goto pop_one;
2809+
2810+
if (bit(xev->rwm->m4flags, RWL_P4_TRIGRAD))
2811+
{
2812+
resdval = cos(cstak[i-1].dval);
2813+
}
2814+
else
2815+
{
2816+
resdval = cos(cstak[i-1].dval) * (M_PI / 180.0);
2817+
}
2818+
resival = (sb8)round(resdval);
2819+
if (bit(xev->tflags,RWL_THR_DEVAL))
2820+
rwldebugcode(xev->rwm, loc, "at %d: cos(%.2f) = %.2f", i, cstak[i-1].dval, resdval);
2821+
rtyp = RWL_TYPE_DBL;
2822+
goto finish_one_math;
2823+
break;
2824+
2825+
27532826
case RWL_STACK_ERLANG:
27542827
/*
27552828
Assuming ran is uniform random number generator

src/rwllexer.l

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* History
1313
*
14+
* johnkenn 02-nov-2023 - $trigonometry:degrees and $trigonometry:radians directives
1415
* bengsig 21-sep-2023 - $errordetail:on directive
1516
* bengsig 13-sep-2023 - ampersand replacement
1617
* bengsig 6-sep-2023 - sql logging
@@ -443,6 +444,8 @@
443444
"nostatistics" if (!rwm->ifdirbit) return RWL_T_NOSTATISTICS;
444445
/*LEXTAG:RWL_T_SQRT*/
445446
"sqrt" if (!rwm->ifdirbit) return RWL_T_SQRT;
447+
"sin" if (!rwm->ifdirbit) return RWL_T_SIN;
448+
"cos" if (!rwm->ifdirbit) return RWL_T_COS;
446449
/*LEXTAG:RWL_T_INSTRB*/
447450
"instr" {
448451
rwlerror(rwm, RWL_ERROR_CURRENTLY_AS, "instr", "instrb");
@@ -653,19 +656,8 @@
653656
#endif
654657
if (!rwm->ifdirbit) return RWL_T_QUERYNOTIFICATION;
655658
}
656-
657-
"sin" |
658-
"cos" {
659-
rwlerror(rwm, RWL_ERROR_NOT_YET_IMPL, yytext);
660-
// return something so compilation continues
661-
if (!rwm->ifdirbit) return RWL_T_ERLANG;
662-
}
663-
664-
"atan2" {
665-
rwlerror(rwm, RWL_ERROR_NOT_YET_IMPL, yytext);
666-
// return something so compilation continues
667-
if (!rwm->ifdirbit) return RWL_T_UNIFORM;
668-
}
659+
/*LEXTAG:RWL_T_ATAN2*/
660+
"atan2" if (!rwm->ifdirbit) return RWL_T_ATAN2;
669661

670662
"|" |
671663
"&" |
@@ -1581,7 +1573,12 @@
15811573
rwm->sqllogfile = 0;
15821574
}
15831575
}
1584-
1576+
\$trigonometry:radian[s]? {
1577+
if (!rwm->ifdirbit) bis(rwm->m4flags, RWL_P4_TRIGRAD);
1578+
}
1579+
\$trigonometry:degree[s]? {
1580+
if (!rwm->ifdirbit) bic(rwm->m4flags, RWL_P4_TRIGRAD);
1581+
}
15851582
\$ |
15861583
\$[^ \t\r\n] { // bad directive
15871584
char idbuf[100], c;

src/rwlmisc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4485,7 +4485,7 @@ void rwlpfeng(rwl_main *rwm
44854485
ub4 rwldebugconv(rwl_main * rwm
44864486
, text * arg)
44874487
{
4488-
// Mapping strcut for for the debug names
4488+
// Mapping struct for for the debug names
44894489
struct rwl_debugmap
44904490
{
44914491
text * name;
@@ -4515,7 +4515,7 @@ ub4 rwldebugconv(rwl_main * rwm
45154515

45164516
while (token != NULL)
45174517
{
4518-
// Get the length of the token and convert token to uppercase
4518+
// Get the length of the token and convert token to lowercase
45194519
size_t token_len = rwlstrlen((char *)token);
45204520
for (index = 0; index < token_len; index++)
45214521
{

src/rwlparser.y

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*
1212
* History
1313
*
14+
* johnkenn 06-nov-2023 - trigonometry sin, cos, atan2
1415
* bengsig 25-sep-2023 - ampersand bug fix
1516
* bengsig 20-sep-2023 - list iterator loop
1617
* bengsig 12-sep-2023 - Ampersand replacement
@@ -174,6 +175,7 @@ static const rwl_yt2txt rwlyt2[] =
174175
, {"RWL_T_ASNPLUS", "'+='"}
175176
, {"RWL_T_ASSIGN", "':='"}
176177
, {"RWL_T_AT", "'at'"}
178+
, {"RWL_T_ATAN2", "'atan2'"}
177179
, {"RWL_T_BEGIN", "'begin'"}
178180
, {"RWL_T_BETWEEN", "'between'"}
179181
, {"RWL_T_BIND", "'bind'"}
@@ -188,6 +190,7 @@ static const rwl_yt2txt rwlyt2[] =
188190
, {"RWL_T_CONNECTIONCLASS", "'connectionclass'"}
189191
, {"RWL_T_CONNECTIONPOOL", "'connectionpool'"}
190192
, {"RWL_T_COUNT", "'count'"}
193+
, {"RWL_T_COS", "'cos'"}
191194
, {"RWL_T_CURSORCACHE", "'cursorcache'"}
192195
, {"RWL_T_DATABASE", "'database'"}
193196
, {"RWL_T_DATE", "'date'"}
@@ -285,6 +288,7 @@ static const rwl_yt2txt rwlyt2[] =
285288
, {"RWL_T_SESSIONPOOL", "'sessionpool'"}
286289
, {"RWL_T_SHARDKEY", "'shardkey'"}
287290
, {"RWL_T_SHIFT", "'shift'"}
291+
, {"RWL_T_SIN", "'sin'"}
288292
, {"RWL_T_SPRINTF", "'sprintf'"}
289293
, {"RWL_T_SQL", "'sql'"}
290294
, {"RWL_T_SQL_ID", "'sql_id'"}
@@ -491,6 +495,7 @@ rwlcomp(rwlparser_y, RWL_GCCFLAGS)
491495
%token RWL_T_STRING_CONST RWL_T_IDENTIFIER RWL_T_INTEGER_CONST RWL_T_DOUBLE_CONST RWL_T_PRINTF
492496
%token RWL_T_PIPEFROM RWL_T_PIPETO RWL_T_RSHIFTASSIGN RWL_T_GLOBAL RWL_T_QUERYNOTIFICATION
493497
%token RWL_T_NORMALRANDOM RWL_T_STATISTICSONLY RWL_T_CEIL RWL_T_TRUNC RWL_T_FLOOR RWL_T_LOBPREFETCH
498+
%token RWL_T_SIN RWL_T_COS RWL_T_ATAN2
494499

495500
// standard order of association
496501
%left RWL_T_CONCAT
@@ -1582,6 +1587,9 @@ identifier_or_constant:
15821587
| RWL_T_FLOOR '(' expression ')' { rwlexprpush0(rwm,RWL_STACK_FLOOR); }
15831588
| RWL_T_ROUND '(' expression ')' { rwlexprpush0(rwm,RWL_STACK_ROUND); }
15841589
| RWL_T_SQRT '(' expression ')' { rwlexprpush0(rwm,RWL_STACK_SQRT); }
1590+
| RWL_T_SIN '(' expression ')' { rwlexprpush0(rwm,RWL_STACK_SIN); }
1591+
| RWL_T_COS '(' expression ')' { rwlexprpush0(rwm,RWL_STACK_COS); }
1592+
| RWL_T_ATAN2 '(' expression ',' expression ')' { rwlexprpush0(rwm,RWL_STACK_ATAN2); }
15851593
| RWL_T_LENGTHB '(' concatenation ')' { rwlexprpush0(rwm,RWL_STACK_LENGTHB); }
15861594
| RWL_T_INSTRB '(' concatenation ',' concatenation')'
15871595
{ rwlexprpush0(rwm,RWL_STACK_INSTRB2); }

test/362.rwl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
double val := 178, x, y;
2+
3+
double atan_x := -7, atan_y := 7, atan_res;
4+
x := sin(val);
5+
y := cos(val);
6+
atan_res := atan2(atan_y, atan_x);
7+
printline x, y, atan_res;
8+
9+
$trigonometry:degree
10+
11+
x := sin(val);
12+
y := cos(val);
13+
atan_res := atan2(atan_y, atan_x);
14+
printline x, y, atan_res;
15+
16+
$trigonometry:radian
17+
18+
x := sin(val);
19+
y := cos(val);
20+
atan_res := atan2(atan_y, atan_x);
21+
printline x, y, atan_res;
22+
23+
$trigonometry:degrees
24+
25+
x := sin(val);
26+
y := cos(val);
27+
atan_res := atan2(atan_y, atan_x);
28+
printline x, y, atan_res;
29+
30+
$trigonometry:radians
31+
32+
x := sin(val);
33+
y := cos(val);
34+
atan_res := atan2(atan_y, atan_x);
35+
printline x, y, atan_res;

0 commit comments

Comments
 (0)