Topic : Floating point math giving wrong results.

Forum : ARM

Original Post
Post Information Post
August 20, 2010 - 11:32pm
Guest

I am trying to develop my first "application" for a STM32 in Ride7 and simulate it in the integrated SIM_ARM. Unfortunately, I am getting erroneous results from some simple calculations.

First, I declare a few variables as volatile so the compilter does not optimize the test code away. (note: main.h include a #include statement.)

#include "main.h"

//Global Varibales
//---------------------------------------------------------------------------------
volatile float f1=25.2f;
volatile float f2=.707f;
volatile float f3=17.3f;
volatile float f4;
volatile float f5;

Then, in the main subroutine I do a few test math operations:

/// Main function. 
int main(void)
{
    // Most of the peripherals are connected to APB2.  Turn on the
    // clocks for the interesting peripherals
    RCC->APB2ENR = 0
        // Turn on USART1
        | RCC_APB2ENR_USART1EN
        // Turn on IO Port A
        | RCC_APB2ENR_IOPAEN
        // Turn on IO Port B
        | RCC_APB2ENR_IOPBEN
        // Turn on the alternate function block
        | RCC_APB2ENR_AFIOEN;

    // Put Port A pins 8 through 15 into alternate function/50 MHz
    // mode.
    GPIOA->CRH = 0xBBBBBBBB;

    // Put PB0 into push pull 50 MHz mode
    GPIOB->CRL = 0x03;

    //add
    f3=(f2+f1);
    
    //mult
    f3=(f2*f1);

    //divide
    f4=17.5/(f1);
    
    //trig
    f3=sin(f2);

    f5=atan(f4);

    //more code deleted that continues initialization and begins infinite loop

}

The project compiles and links successfully.

When I debug in SIM-ARM and step through the code the results are...
addition: 25.2+.707=25.907 (good)
multipication: 25.2*.707=8 (not good)
division: 17.5/25.2=0.6944 (good)
trig: sin(.707)=-13.9987 (not good)

From stepping throught the dissasembly, it looks like the following calls were made for the various opetations...
addition: __addsf3
multipication: __aeabi_fmul
division: __aeabi_fmul
sin: __kernel_sin

I am entirely new to Ride7 and the cortex processors in general (experienced with avr-gcc and AVR processors), so I am sure I just don't have something set up correctly... but I have no Idea what.

Thanks in advance for your help!

Replies
Post Information Post
+1
0
-1
August 23, 2010 - 10:49am
Raisonance Support Team

Hi,

Thanks for your detailed report. We reproduced the problem in our Labs.
Our engineers are working on the issue. When using hardware debugging (RLink, REva or STM32-Primer), the code works fine, which might be an acceptable workaround for you.

This will be corrected in the next release of RKit-ARM through.
Regards,