Thread: Math help?

  1. #1
    Registered User
    Join Date
    Mar 2020
    Posts
    79

    Math help?

    I have the following code:

    Code:
    #include <msp430.h>#include "stdlib.h"
    #include "stdio.h"
    
    
        const char frq[8], br[8];
        long f, b;
        char *frqPtr, *brPtr;
        int d;
        float interim;
    
    
    void main()
    {
        strcpy(frq, "16000000");
        strcpy(br, "115200");
        f = atol(frq);
        b = atol(br);
        interim = f/b/16;
        d = (int)((interim - (int)interim) * 16);
    }
    After I run I get the following from the debugger:
    interim float 8.0 0x002018
    d int 0 0x00201C
    f long 16000000 0x002014
    b long 115200 0x002010

    Can someone tell me how to get interim = 8.68055555556 and d = 10?

    Problem solved: change f, b to float and cast
    Last edited by ridgerunnersjw; 10-31-2020 at 04:14 PM. Reason: Solved

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    866
    Are you sure? The final value must be 8.6805553436279296875 in single precision (8.68055555556 cannot be represented in any floating point precision).

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,661
    > void main()
    main returns an int.

    > strcpy(frq, "16000000");
    1. You don't include string.h
    2. frq is declared const, so you can't copy to it.
    3. frq isn't large enough anyway, so buffer overflow.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. <math.h> help
    By evildotaing in forum C++ Programming
    Replies: 14
    Last Post: 02-02-2012, 05:38 PM
  2. Math
    By knightjp in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 04-01-2009, 05:36 PM
  3. math.h
    By derrickn in forum C Programming
    Replies: 3
    Last Post: 08-04-2005, 08:43 PM
  4. Basic Math Problem. Undefined Math Functions
    By gsoft in forum C Programming
    Replies: 1
    Last Post: 12-28-2004, 03:14 AM
  5. Math Help
    By CAP in forum C Programming
    Replies: 2
    Last Post: 08-19-2002, 12:03 AM

Tags for this Thread