~eda-qa/leaflang/cpp

« back to all changes in this revision

Viewing changes to share/std_library/std/float_to_ascii.leaf

  • Committer: edA-qa mort-ora-y
  • Date: 2017-08-19 06:03:00 UTC
  • mfrom: (100.1.22 misc)
  • Revision ID: eda-qa@disemia.com-20170819060300-209dwd5884343mi0
merging miscelaneous changes, mainly platform improvements

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
@import("sprintf") multi sprintf : ( : raw_array「abi_char」, : raw_array「abi_char」, : abi_var_args ) -> ( : abi_int ) raw no_throw
 
2
 
 
3
@export defn print = (x : float) -> {
 
4
        print( float_to_ascii(x) )
 
5
}
 
6
 
 
7
@export defn float_to_ascii = (x : float)-> {
 
8
        var buf = type「 array「abi_char」 」(128)
 
9
        var q = type「 array「abi_char」 」(3)
 
10
        q#0 = lossy(code_val('%'))
 
11
        q#1 = lossy(code_val('g'))
 
12
        q#2 = 0
 
13
        ignore sprintf( buf, q, [x] )
 
14
        
 
15
        var len = abi_strlen(buf)
 
16
        var str = type「 array「char」 」(len)
 
17
        for at in range(0,len) {
 
18
                str#at = char_val(buf#at)
 
19
        }
 
20
        
 
21
        return str
 
22
}
 
23
 
 
24
var abi_strlen = (buf : array「abi_char」) -> {
 
25
        var x = 0
 
26
        loop buf#x != 0 ? {
 
27
                x = x + 1
 
28
        }
 
29
        return x
 
30
}
 
31
 
 
32
@import("frexp") defn frexp : ( : abi_double, : abi_int value_ptr ) -> ( : abi_double ) raw no_throw
 
33
@import("ldexp") defn ldexp : ( : abi_double, : abi_int ) -> ( : abi_double ) raw no_throw
 
34
@import("nextafter") defn nextafter : ( : abi_double, : abi_double ) -> ( : abi_double ) raw no_throw
 
35
 
 
36
@export defn split_float = (x : float) -> ( significand : float, exponent : integer ) {
 
37
        var xa = (x : abi_double)
 
38
        var exp : abi_int
 
39
        var sig = frexp( xa, exp )
 
40
        return ( sig, exp )
 
41
}
 
42
 
 
43
@export defn pow2 = ( x: float, e : integer ) -> ( : float ) {
 
44
        return ldexp( x, lossy(e) )
 
45
}
 
46
 
 
47
@export defn next_float = ( x : float, direction : float ) -> ( : float ) {
 
48
        return nextafter( x, direction )
 
49
}