Syntax for pow function using inline C

Posted on
  • Sun 2018.08.26

    I am attempting to write some simple inline C functions. Was able to get the first snippet 'How do I use it' to compile and run.

    http://www.espruino.com/InlineC

    I'm now attempting the 'C' pow() function but wonder where to place the #include statement
    #include
    Wouldn't you know it, this post machine wont allow the lt math.h gl symbology

    #include <math.h>



    Not under: either
    http://www.espruino.com/Compilation

    It can't be placed outside the var c = E.compiledC( block as it's not recognized as Javascript, and placing it inside results in this compiler msg:

    COMPILER MESSAGE
    ----------------------------------------­-----------
    out4261837251.cpp:60:1: error: stray '#' in program
     [#include](http://forum.espruino.com/sea­rch/?q=%23include) <math.h>
     ^
    out4261837251.cpp:60:2: error: 'include' does not name a type
     [#include](http://forum.espruino.com/sea­rch/?q=%23include) <math.h>
    
                                     COMPILER MESSAGE
    ----------------------------------------­-----------
    out1319183279.cpp: In function 'int calc(int)':
    out1319183279.cpp:71:26: error: 'pow' was not declared in this scope
      float dP = pow(dR,dG);
    
    


    var cg = E.compiledC(`
    // int calc(int)
    
    [#include](http://forum.espruino.com/sea­rch/?q=%23include)  <math.h>
    
    int calc(int n){
     float dR = float(n) / 255.0;
     float dP = pow(dR,dG);
    


    var cg = E.compiledC(`
    // int calc(int)
    #include <math.h>
    int calc(int n){
    float dR = float(n) / 255.0;
    float dP = pow(dR,dG);

    Need the insight of someone that's been down this path.
    A pow er user ;-)

    Thankx

  • ...since there is no C preprocessor to resolve #define - see Caveats in 'Inline C code', there is also no #include. The issue is that the service the way it is implemented right now would not know from where to include from... May be this could change when callbakc-url could be passed and you can serve your things to include by a (public) Web server... (you are exposing your dev source thru http).

  • Sun 2018.08.26

    Yeah, read that but on:

    http://www.espruino.com/Compilation

    What works and what doesn't?
    Works
    Maths, string operations

    What part of using a math function isn't 'Maths' ?

    and wasn't sure if an extension was similar to a module

    http://www.espruino.com/Extending+Esprui­no+1

    #include referenced there, so went on a hunch



    Wonder if I could accomplish the same task with a function that iterates using brute force += multiplication? Hmmmm

  • http://www.espruino.com/Compilation is about compiling JS code, not inline C which is http://www.espruino.com/InlineC

    http://www.espruino.com/Extending+Esprui­no+1 is about compiling your own version if Espruino - in which case you can include what you want.

    The preprocessor is disabled because:

    • it's a potential security hole (#include "/etc/passwd")
    • including things will almost certainly bring in a lot of extra code and/or memory requirements that will probably use enough RAM that it makes it unusable anyway.

    I guess you're just compiling a pow function for fun? Because there's one already in Math.pow.

    If I were doing it, I'd:

    As mentioned in InlineC though, you may have similar issues dealing with double arithmetic though, since the double maths functions won't get built in by default.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Syntax for pow function using inline C

Posted by Avatar for Robin @Robin

Actions