element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum Return of Natural log
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 25 replies
  • Subscribers 405 subscribers
  • Views 4555 views
  • Users 0 members are here
Related

Return of Natural log

aureta
aureta over 10 years ago

Hi, I need to have returned in arduino the natural log of a random generated x number. How should the line code for obtaining the ln number should look like. Other forums mention the inclusion of a math.h library,

etc, but so far I am not getting what I need. I really appreciate help in this matter. Alex.

  • Sign in to reply
  • Cancel

Top Replies

  • balearicdynamics
    balearicdynamics over 10 years ago +2
    In your code you user ln10() ... In the cited reference, the two logarithm calculation methods are double log (double __x) // natural logarithm of x double log10 (double __x) // logarithm of x to base…
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to gadget.iom +2
    Paul, only use double instead of long. Better avoiding too many casting and implicit conversions I think. That's ok, if the author has clear his request.
  • gadget.iom
    gadget.iom over 10 years ago +2
    This is the latest version of the code I have worked with: double y = 0; void setup(){ Serial.begin(9600); randomSeed(analogRead(0)); } void loop() { double randNumber = random(300); y = log(randNumber…
  • gadget.iom
    gadget.iom over 10 years ago

    Take a look at reply 4 here: Log functions not included in list of math functio

    This may solve your issues.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • aureta
    aureta over 10 years ago in reply to gadget.iom

    Hi, I  am including a snapshot of the code and serial monitor output. Still log of the x random number isn´t coming out with this code. I´ll appreciate help.  image

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • clem57
    clem57 over 10 years ago in reply to aureta

    Looking at header it returns double not float. Arduino - H

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • balearicdynamics
    balearicdynamics over 10 years ago

    In your code you user ln10() ...

     

    In the cited reference, the two logarithm calculation methods are

     

     

    double log (double __x) // natural logarithm of x

    double log10 (double __x) // logarithm of x to base 10.

     

    So the natural funciton is log, not log10() or I am wrong ?

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • Cancel
  • clem57
    clem57 over 10 years ago in reply to balearicdynamics

    Correct Enrico. Also you need to send a double and not an int value.

    Clem

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • aureta
    aureta over 10 years ago in reply to clem57

    sorry,  but could you please tell how should the code look like. I am new in Arduino...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to aureta

    Alejandro, please post your code and not a screenshot, so someone of us can replay with some correction...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • aureta
    aureta over 10 years ago in reply to balearicdynamics

    Thank you, here´s the code...

     

    float y= 0;

     

     

    void setup(){

      Serial.begin(9600);

     

    float lognx= 0;

     

      randomSeed(analogRead(0));

    }

     

    void loop() {

      // print a random number from 0 to 299

      randNumber = random(300);

    y= log10(randNumber);

     

      // print a random number from 10 to 19

      randNumber = random(10, 20);

      Serial.print(randNumber); Serial.print("\t");

      Serial.println(y);

    delay(1000);

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • balearicdynamics
    balearicdynamics over 10 years ago in reply to aureta

    Hello Alejandro,

     

    First of all, please read how to use the editor, so we don't loose so much time finding comments in two posts and formatting your code.

    I put your code below with my comments, so if you like you can try it

     

    #include <math.h>

     

    // Pay atention that you should have the math library in your Arduino IDE library path

     

    double y=0; //float y= 0;

     

    // Setup

    void setup(){

      Serial.begin(9600);

    double lognx = 0; // float lognx= 0;

    randomSeed(analogRead(0));

     

    }

     

    //////////////////////////

    // Loop

    //////////////////////////

    void loop() {

      // as we need a double to calc the ln while the random() generates a long interer it is better to cast it

      y = log((double)random(300)); // y= log10(randNumber);

     

      // print a random number from 10 to 19

      long randNumber = random(10, 20);

     

     

      Serial.print(randNumber); Serial.print("\t");

      Serial.println(y);

     

     

      delay(1000);

    }

     

    Please try this code.

     

    A note on your logic that sound strange to me: why do you calculate the log on the random number 0-300 while then print another number between 10 and 19 ? Just to be sure that this is what you want.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • Cancel
  • aureta
    aureta over 10 years ago in reply to balearicdynamics

    what I want is arduino to pick up a random number and for it, return their natural log. ...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube