Skip to content
Snippets Groups Projects
Commit 481a688e authored by Leo's avatar Leo
Browse files

fix: maybe-statments, allow float percentages below 1. (e.g 0.14%) + additional maybe example

parent 1bb9d646
No related branches found
No related tags found
No related merge requests found
......@@ -2,7 +2,7 @@
<configuration default="false" name="Styve" type="Application" factoryName="Application" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Styve" />
<module name="styve" />
<option name="PROGRAM_PARAMETERS" value="examples" />
<option name="PROGRAM_PARAMETERS" value="examples/13_kanskje_2.styve" />
<method v="2">
<option name="Make" enabled="true" />
</method>
......
--- Loop 650 000 times, and count how many times the maybe statement triggers.
--- Then we show the percentage of times it triggered in practice, out of the total iterations.
--- Total iterations
Arne Arne Styve var Arne 650000 Styve
--- Counter for the maybe statement
Arne Arne Arne probCount_one Arne 0 Styve
Arne Arne Arne probCount_two Arne 0 Styve
Arne Arne Arne probCount_three Arne 0 Styve
--- Loop 650 000 times
Gjenta Arne Arne Arne i Arne 1 Styve i Underflow var Styve i++ 8=
Kanskje 0.04321% 8=
--- Increment the counter
probCount_one++ Styve
=D Styve
Kanskje 50.5050% 8=
--- Increment the counter
probCount_two++ Styve
=D Styve
Kanskje 0.15 8=
--- Increment the counter
probCount_three++ Styve
=D Styve
=D
--- Show the result
--- STANDUP("The probability triggered: " + probCount + " times, out of " + var + " iterations. That's " + (probCount / var * 100) + "%.") Styve
STANDUP("The probability triggered for (0.04321%): " + probCount_one + " times, out of " + var + " iterations. That's " + (probCount_one / var * 100) + "%.") Styve
STANDUP("The probability triggered for (50.5050%): " + probCount_two + " times, out of " + var + " iterations. That's " + (probCount_two / var * 100) + "%.") Styve
STANDDOWN("The probability triggered for (15%): " + probCount_three + " times, out of " + var + " iterations. That's " + (probCount_three / var * 100) + "%.") Styve
STANDUP("Now, thats pretty accurate!") Styve
\ No newline at end of file
......@@ -939,6 +939,11 @@ public class Parser {
// We are working with a percentage value as 12%, 25.34%, etc.
op = this.expect(TokenType.BinaryOperator, "Expected a % operator for the maybe percentage value, otherwise use a decimal value (0-1)");
percentageStmt = new NumericLiteral(percentageStartToken, op, percentage.getFloatValue() / 100.0f);
} else {
if (this.atType(TokenType.BinaryOperator) && this.peek().value.equals("%")) {
// We are working with a percentage value as 0.12%, 0.25%, etc.
op = this.consume();
percentageStmt = new NumericLiteral(percentageStartToken, op, percentage.getFloatValue() / 100.0f);
} else {
// We are working with a decimal value as 0.12, 0.25, etc.
if (percentage.getFloatValue() < 0) {
......@@ -946,6 +951,7 @@ public class Parser {
}
percentageStmt = percentage;
}
}
} else {
// Identifier
IdentifierExpr ident = (IdentifierExpr) percentageNumeric;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment