• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rokucommunity / brs / #305

18 Jan 2024 09:05PM UTC coverage: 91.463% (+5.2%) from 86.214%
#305

push

TwitchBronBron
0.45.4

1796 of 2095 branches covered (85.73%)

Branch coverage included in aggregate %.

5275 of 5636 relevant lines covered (93.59%)

8947.19 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.12
/src/stdlib/Math.ts
1
import { Callable, ValueKind, Int32, Float, StdlibArgument } from "../brsTypes";
131✔
2
import { Interpreter } from "../interpreter";
3

4
/** Returns the absolute value of a float. */
5
export const Abs = new Callable("Abs", {
131✔
6
    signature: {
7
        args: [new StdlibArgument("x", ValueKind.Float)],
8
        returns: ValueKind.Float,
9
    },
10
    impl: (interpreter: Interpreter, x: Float) => new Float(Math.abs(x.getValue())),
4✔
11
});
12

13
/*
14
 * Returns the integer as a 32-bit float.
15
 * ** NOTE: the function name implies it makes a 64-bit float, but the docs say
16
 *     it currently returns a 32-bit float, but may return a 64-bit float in the future.
17
 */
18
export const Cdbl = new Callable("Cdbl", {
131✔
19
    signature: {
20
        args: [new StdlibArgument("x", ValueKind.Int32)],
21
        returns: ValueKind.Float,
22
    },
23
    impl: (interpreter: Interpreter, x: Int32) => new Float(x.getValue()),
2✔
24
});
25

26
/** Returns an integer from a float rounding up from midpoints */
27
export const Cint = new Callable("Cint", {
131✔
28
    signature: {
29
        args: [new StdlibArgument("x", ValueKind.Float)],
30
        returns: ValueKind.Int32,
31
    },
32
    impl: (interpreter: Interpreter, x: Int32) => new Int32(Math.round(x.getValue())),
6✔
33
});
34

35
/** Returns the integer as a 32-bit float. */
36
export const Csng = new Callable("Csng", {
131✔
37
    signature: {
38
        args: [new StdlibArgument("x", ValueKind.Int32)],
39
        returns: ValueKind.Float,
40
    },
41
    impl: (interpreter: Interpreter, x: Int32) => new Float(x.getValue()),
2✔
42
});
43

44
/** Returns an integer from a float removing fractional parts. */
45
export const Fix = new Callable("Fix", {
131✔
46
    signature: {
47
        args: [new StdlibArgument("x", ValueKind.Float)],
48
        returns: ValueKind.Int32,
49
    },
50
    impl: (interpreter: Interpreter, x: Int32) => new Int32(Math.trunc(x.getValue())),
3✔
51
});
52

53
/** Returns an integer from a float. */
54
export const Int = new Callable("Int", {
131✔
55
    signature: {
56
        args: [new StdlibArgument("x", ValueKind.Float)],
57
        returns: ValueKind.Int32,
58
    },
59
    impl: (interpreter: Interpreter, x: Int32) => new Int32(Math.floor(x.getValue())),
4✔
60
});
61

62
function SgnImpl(interpreter: Interpreter, x: Int32 | Float) {
63
    let val = x.getValue();
2✔
64
    if (val > 0.0) return new Int32(1);
2✔
65
    else if (val < 0.0) return new Int32(-1);
1!
66
    else return new Int32(0);
×
67
}
68

69
/** Returns -1 if parameter is negative, 0 if zero, and 1 if positive. */
70
export const Sgn = new Callable(
131✔
71
    "Sgn",
72
    {
73
        signature: {
74
            args: [new StdlibArgument("x", ValueKind.Float)],
75
            returns: ValueKind.Int32,
76
        },
77
        impl: SgnImpl,
78
    },
79
    {
80
        signature: {
81
            args: [new StdlibArgument("x", ValueKind.Int32)],
82
            returns: ValueKind.Int32,
83
        },
84
        impl: SgnImpl,
85
    }
86
);
87

88
/** Returns the arc-tangent (in radians) of a float. */
89
export const Atn = new Callable("Atn", {
131✔
90
    signature: {
91
        args: [new StdlibArgument("x", ValueKind.Float)],
92
        returns: ValueKind.Float,
93
    },
94
    impl: (interpreter: Interpreter, x: Float) => new Float(Math.atan(x.getValue())),
4✔
95
});
96

97
/** Returns the cosine of a float (argument must be provided in radians). */
98
export const Cos = new Callable("Cos", {
131✔
99
    signature: {
100
        args: [new StdlibArgument("x", ValueKind.Float)],
101
        returns: ValueKind.Float,
102
    },
103
    impl: (interpreter: Interpreter, x: Float) => new Float(Math.cos(x.getValue())),
3✔
104
});
105

106
/** Returns the sine of a float (argument must be provided in radians). */
107
export const Sin = new Callable("Sin", {
131✔
108
    signature: {
109
        args: [new StdlibArgument("x", ValueKind.Float)],
110
        returns: ValueKind.Float,
111
    },
112
    impl: (interpreter: Interpreter, x: Float) => new Float(Math.sin(x.getValue())),
3✔
113
});
114

115
/** Returns the tangent float (argument must be provided in radians). */
116
export const Tan = new Callable("Tan", {
131✔
117
    signature: {
118
        args: [new StdlibArgument("x", ValueKind.Float)],
119
        returns: ValueKind.Float,
120
    },
121
    impl: (interpreter: Interpreter, x: Float) => new Float(Math.tan(x.getValue())),
3✔
122
});
123

124
/** Returns the natural exponent of a float. */
125
export const Exp = new Callable("Exp", {
131✔
126
    signature: {
127
        args: [new StdlibArgument("x", ValueKind.Float)],
128
        returns: ValueKind.Float,
129
    },
130
    impl: (interpreter: Interpreter, x: Float) => new Float(Math.exp(x.getValue())),
2✔
131
});
132

133
/** Returns the log of a float. */
134
export const Log = new Callable("Log", {
131✔
135
    signature: {
136
        args: [new StdlibArgument("x", ValueKind.Float)],
137
        returns: ValueKind.Float,
138
    },
139
    impl: (interpreter: Interpreter, x: Float) => new Float(Math.log(x.getValue())),
2✔
140
});
141

142
/** Returns the square root of a float. */
143
export const Sqr = new Callable("Sqr", {
131✔
144
    signature: {
145
        args: [new StdlibArgument("x", ValueKind.Float)],
146
        returns: ValueKind.Float,
147
    },
148
    impl: (interpreter: Interpreter, x: Float) => new Float(Math.sqrt(x.getValue())),
2✔
149
});
150

151
/**
152
 * Returns a random number in a given range. If the range is zero, a random
153
 * float between [0,1) is returned. If the range is a positive number, a
154
 * random integer between 1 and that number is returned (inclusive is returned).
155
 *
156
 * **NOTE:** the float returned is in the range [0,1) to match the javascript
157
 *     implementation, while the brightscript specification calls for (0,1).
158
 *     This should be okay in practice, but if this is necessary a more complicated
159
 *     implementation will be necessary.
160
 */
161
export const Rnd = new Callable("Rnd", {
131✔
162
    signature: {
163
        args: [new StdlibArgument("range", ValueKind.Int32)],
164
        returns: ValueKind.Dynamic,
165
    },
166
    impl: (interpreter: Interpreter, range: Int32) => {
167
        if (range.getValue() === 0) return new Float(Math.random());
4✔
168
        else return new Int32(Math.floor(Math.random() * range.getValue() + 1));
3✔
169
    },
170
});
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc