summary refs log tree commit diff stats
path: root/lib/pure/math.nim
diff options
context:
space:
mode:
authorCharlie <bartoc@umich.edu>2014-05-18 15:13:37 -0400
committerCharlie <bartoc@umich.edu>2014-05-18 15:13:37 -0400
commit8a183dac78a5076d421dcbff263c6d163fe2a7fc (patch)
treee8fda0958ff91878c04fb9c2ac113d3c467cd225 /lib/pure/math.nim
parente54ab22bf9a67353e5a70f56e7801624d68ca4f5 (diff)
downloadNim-8a183dac78a5076d421dcbff263c6d163fe2a7fc.tar.gz
added random(max: float): float support to windows
Diffstat (limited to 'lib/pure/math.nim')
-rw-r--r--lib/pure/math.nim20
1 files changed, 13 insertions, 7 deletions
diff --git a/lib/pure/math.nim b/lib/pure/math.nim
index e4aecd272..24a2ec7fa 100644
--- a/lib/pure/math.nim
+++ b/lib/pure/math.nim
@@ -135,12 +135,11 @@ proc random*(max: int): int {.gcsafe.}
   ## which initializes the random number generator with a "random"
   ## number, i.e. a tickcount.
 
-when not defined(windows):
-  proc random*(max: float): float {.gcsafe.}
-    ## returns a random number in the range 0..<max. The sequence of
-    ## random number is always the same, unless `randomize` is called
-    ## which initializes the random number generator with a "random"
-    ## number, i.e. a tickcount. This is currently not supported for windows.
+proc random*(max: float): float {.gcsafe.}
+  ## returns a random number in the range 0..<max. The sequence of
+  ## random number is always the same, unless `randomize` is called
+  ## which initializes the random number generator with a "random"
+  ## number, i.e. a tickcount. This is currently not supported for windows.
 
 proc randomize*() {.gcsafe.}
   ## initializes the random number generator with a "random"
@@ -205,7 +204,14 @@ when not defined(JS):
     proc drand48(): float {.importc: "drand48", header: "<stdlib.h>".}
     proc random(max: float): float =
       result = drand48() * max
-    
+  when defined(windows):
+    proc random(max: float): float =
+      # we are hardcodeing this because
+      # importcing macros is extremely problematic
+      # and because the value is publicly documented
+      # on MSDN and very unlikely to change
+      const rand_max = 32767
+      result = (float(rand()) / float(rand_max)) * max
   proc randomize() =
     randomize(cast[int](epochTime()))
 
font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
import macros, parseutils

# Generate tags
macro make(names: untyped{nkBracket}): untyped =
    result = newStmtList()

    for i in 0 .. names.len-1:
        result.add newProc(
            name   = ident($names[i]).postfix("*"),
            params = [
                ident("string"),
                newIdentDefs(
                    ident("content"),
                    ident("string")
                )
            ],
            body = newStmtList(
                parseStmt("reindent(content)")
            )
        )


iterator lines(value: string): string =
    var i = 0
    while i < value.len:
        var line: string
        inc(i, value.parseUntil(line, 0x0A.char, i) + 1)
        yield line


proc reindent*(value: string, preset_indent = 0): string =
    var indent = -1

    # Detect indentation!
    for ln in lines(value):
        var read = ln.skipWhitespace()

        # If the line is empty, ignore it for indentation
        if read == ln.len: continue

        indent = if indent < 0: read
                 else: min(indent, read)

    # Create a precursor indent as-needed
    var precursor = newString(0)
    for i in 1 .. preset_indent:
        precursor.add(' ')

    # Re-indent
    result = newString(0)

    for ln in lines(value):
        var value = ln.substr(indent)

        result.add(precursor)

        if value.len > 0:
            result.add(value)
            result.add(0x0A.char)

    return result


#Define tags
make([ html, xml, glsl, js, css, rst ])


when isMainModule:
    ## Test tags

    const script = js"""
        var x = 5;
        console.log(x.toString());
    """

    const styles = css"""
        .someRule {
            width: 500px;
        }
    """

    const body = html"""
        <ul>
            <li>1</li>
            <li>2</li>
            <li>
                <a hef="#google">google</a>
            </li>
        </ul>
    """

    const info = xml"""
        <item>
            <i>1</i>
            <i>2</i>
        </item>
    """

    const shader = glsl"""
        void main()
        {
            gl_Position = gl_ProjectionMatrix
                        * gl_ModelViewMatrix
                        * gl_Vertex;
        }
    """


    echo script
    echo styles
    echo body
    echo info
    echo shader