ソースを参照

Add more examples, fix XKCD todo

David Peter 2 年 前
コミット
bcfcabb597

+ 1 - 0
book/build.sh

@@ -34,6 +34,7 @@ generate_example musical_note_frequency "Musical note frequency" true
 generate_example body_mass_index "Body mass index" true
 generate_example pipe_flow_rate "Flow rate in a pipe" true
 generate_example recipe "Recipe" true
+generate_example xkcd_2585 "XKCD 2585" false
 
 generate_example numbat_syntax "Syntax overview" false
 

+ 1 - 0
book/src/SUMMARY.md

@@ -13,6 +13,7 @@
     - [Body mass index](./example-body_mass_index.md)
     - [Flow rate in a pipe](./example-pipe_flow_rate.md)
     - [Recipe](./example-recipe.md)
+    - [XKCD 2585](./example-xkcd_2585.md)
 
 # Numbat language reference
 

+ 2 - 1
book/src/example-numbat_syntax.md

@@ -48,7 +48,8 @@ meter per second  # 'per' keyword can be used for division
 3 in -> cm        # Unit conversion, can also be → or ➞
 3 in to cm        # Unit conversion with the 'to' keyword
 
-cos(pi/3)         # Call mathematical functions
+cos(pi/3 + pi)    # Call mathematical functions
+pi/3 + pi // cos  # equivalent, useful in REPL
 
 # 4. Variable definitions
 

+ 38 - 0
book/src/example-xkcd_2585.md

@@ -0,0 +1,38 @@
+<!-- This file is autogenerated! Do not modify it -->
+
+# XKCD 2585
+
+``` numbat
+# https://xkcd.com/2585/
+#
+# I can ride my bike at 45 mph.
+# If you round.
+
+17 mph
+
+ans -> meters/sec    // round
+ans -> knots         // round
+ans -> fathoms/sec   // round
+ans -> furlongs/min  // round
+ans -> fathoms/sec   // round
+ans -> kph           // round
+ans -> knots         // round
+ans -> kph           // round
+ans -> furlongs/hour // round
+ans -> mph           // round
+ans -> m/s           // round
+ans -> furlongs/min  // round
+ans -> yards/sec     // round
+ans -> fathoms/sec   // round
+ans -> m/s           // round
+ans -> mph           // round
+ans -> furlongs/min  // round
+ans -> knots         // round
+ans -> yards/sec     // round
+ans -> fathoms/sec   // round
+ans -> knots         // round
+ans -> furlongs/min  // round
+ans -> mph           // round
+
+assert_eq(ans, 45 mph)
+```

+ 0 - 89
book/src/syntax-overview.md

@@ -1,89 +0,0 @@
-# Syntax overview
-
-``` numbat
-# This is a line comment. It can span over
-# multiple lines
-
-# 1. Imports
-
-use prelude       # This is not necessary. The 'prelude'
-                  # module will always be loaded upon startup
-
-use physics::temperature_conversion   # Load a specific module
-
-# 2. Numbers
-
-12345       # integer notation
-12_345      # optional decimal separators
-
-0.234       # floating point notation
-.234        # without the leading zero
-
-1.234e15    # scientific notation
-1.234e+15
-1e-9
-1.0e-9
-
-0x2A        # hexadecimal
-0o52        # octal
-0b101010    # binary
-
-# 3. Simple expressions
-
-3 + (4 - 3)       # Addition and subtraction
-
-1920 / 16 * 9     # Multiplication, division
-1920 ÷ 16 × 9     # Unicode-style, '·' is also multiplication
-2 pi              # Whitespace is implicit multiplication
-meter per second  # 'per' keyword can be used for division
-
-2^3               # Exponentiation
-2**3              # Python-style
-2³                # Unicode exponents
-
-17 % 4            # Modulo
-
-3 in -> cm        # Unit conversion, can also be → or ➞
-3 in to cm        # Unit conversion with the 'to' keyword
-
-cos(pi/3)         # Call mathematical functions
-
-# 4. Variable definitions
-
-let x = 4                          # Simple numerical variable
-let y1 = 2 m/s                     # Right hand side can be any expression
-let y2: Speed = 2 m/s              # With optional type annotation
-let y3: Length / Time = 2 m/s      # more complex type annotation
-
-# 5. Function definitions
-
-fn foo(x: Scalar) -> Scalar = 2 * x + 3         # A simple function
-fn speed(s: Length, t: Time) -> Speed = s / t   # Two parameters
-fn my_sqrt<T>(x: T^2) -> T = x^(1/2)            # A generic function
-
-# 6. Dimension definitions
-
-dimension Fame                            # A new base dimension
-dimension Deceleration = Length / Time^2  # A new derived dimension
-
-# 7. Unit definitions
-
-@aliases(quorks)                 # Optional aliases-decorator
-unit quork = 0.35 meter          # A new derived unit
-
-@metric_prefixes                 # Optional decorator to allow 'milliclonk', etc.
-@aliases(ck: short)              # short aliases can be used with short prefixes (mck)
-unit clonk: Time = 0.2 seconds   # Optional type annotation
-
-@metric_prefixes
-@aliases(wh: short)
-unit warhol: Fame                # New base unit for phys. dimension "Fame"
-
-unit thing                       # New base unit with automatically generated
-                                 # base dimension "Thing"
-
-# 8. Procedures
-
-print(2 kilowarhol)              # Print a quantity
-assert_eq(1 ft, 12 in)           # Assert that two quantities are equal
-```

+ 2 - 1
examples/numbat_syntax.nbt

@@ -43,7 +43,8 @@ meter per second  # 'per' keyword can be used for division
 3 in -> cm        # Unit conversion, can also be → or ➞
 3 in to cm        # Unit conversion with the 'to' keyword
 
-cos(pi/3)         # Call mathematical functions
+cos(pi/3 + pi)    # Call mathematical functions
+pi/3 + pi // cos  # equivalent, useful in REPL
 
 # 4. Variable definitions
 

+ 1 - 57
examples/xkcd_2585.nbt

@@ -14,7 +14,7 @@ ans -> kph           // round
 ans -> knots         // round
 ans -> kph           // round
 ans -> furlongs/hour // round
-ans -> mi/h          // round       # TODO: replace mi/h with mph. see below for details
+ans -> mph           // round
 ans -> m/s           // round
 ans -> furlongs/min  // round
 ans -> yards/sec     // round
@@ -30,59 +30,3 @@ ans -> furlongs/min  // round
 ans -> mph           // round
 
 assert_eq(ans, 45 mph)
-
-
-# Unfortunately, if we replace mi/h above with mph, the test fails.
-# The reason lies in the '204 furlongs/hour -> mph' conversion.
-# 204 furlongs are 25.5 miles (exact). 204 furlongs/hour -> mph is
-# therefore supposed to yield 26 mph after rounding. However, in our
-# primitive `Quantity::convert_to` implementation, we first convert
-# to the corresponding base unit (m/s) and then back:
-#
-#    furlongs/hour -> m/s -> mph
-#
-# Minor floating point inaccuracies then lead to a result which is
-# slightly less than 25.5 mph. After rounding, this becomes 25 mph.
-#
-# Improving the conversion algorithm slightly by removing common
-# unit factors (here: 1/hour), improved the situation and now leads
-# to the correct result when using mi/h (mile/hour). However, if we
-# use the `mph` shorthand, our common-unit-factor computation fails
-# and still leads to the wrong 25 mph result.
-#
-# A proper solution to this would probably build a full DAG for all
-# unit definitions of a single physical dimension. The conversion
-# algorithm could then be changed to use shortest paths in those
-# graphs. For example: If both miles and furlongs were defined via
-# the unit foot, we could use foot/hour as the 'base' unit for the
-# conversion instead of meter/second:
-#
-#
-#                meter       second
-#
-#                  ▲            ▲
-#                  │            │
-#                  │            │
-#                  │            │
-#
-#                inch        minute
-#
-#                  ▲            ▲
-#                  │            │
-#                  │            │
-#                  │            │
-#
-#         ┌────► foot         hour
-#         │
-#         │        ▲
-#         │        │
-#         │        │
-#         │        │
-#
-#       mile    furlong
-#
-#
-# Another, potentially simpler solution to this problem could be to
-# introduce a proper type for rationals. This way, floating point
-# inaccuracies would only appear for units with irrational defining
-# conversion factors, like `unit degree = pi / 180 × radian`.