Whats wrong with this calculation/simulation?

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

Post Reply
MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Whats wrong with this calculation/simulation?

Post by MJU »

Hello there,

I've made this AVR flowchart (in Flowcode V6) that gives a result when simulating that is what it should be.
The result of this flowcharts should be: 2456657.0 (and that is what it says on the LCD).
When I send it to the ATMEGA324P it returns 31825.0

This is a big difference. What am I doing wrong?

The only thing I can think of that I should use the fmul and fdiv notations for floats.
Too bad the simulation returns the good result and the AVR gives a different.
Any suggestions?
Attachments
Calc1 .fcfx
(5.76 KiB) Downloaded 233 times

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by medelec35 »

What it appears to be is an issue when just multiplying 367 * 2014 not sure why this is causing an issue?
Maybe you could verify that is the case?
Perhaps Jonny or Ben may know why multiplying those numbers causes answer to be wrong?

I don't believe using fmul will solve this.

I could be wrong of course
Martin

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by JonnyW »

Hello.

In simulation Flowcode will detect that the result of 367 * 2014 is greater than 16 bits and store the result temporarily in a 32-bit integer until it is time to cast it to a float.

The C compile on the AVR may not do this (I believe this is wrong, but more efficient - at the least it should warn you of an overflow when the two values are constants).

Try to add a .0 at the end of your constants when you are dealing with floats. This means neither the C compiler or Flowcode has to make the conversion. If in doubt, be explicit.

So try 367.0 * 1024.0. See if that yields the same result.

Cheers,

Jonny

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by MJU »

Thanks both!!

But sorry to say, I still get a different result in simulating and on my Eblocks.
I've changed all constant into one with decimals (9 becomes 9.0 and so on), but still I expect: 2456657.00000 but I get 31825.000
Calc1.jpg
Calc1.jpg (148.36 KiB) Viewed 6272 times
Any more ideas?

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by MJU »

I found a problem (I think).

When I print the result of the result (a float) in simulation (without converting it to a string), I get the same result on the AVR as on my Eblocks...

So maybe the problem is with the "JDtext = FloatToString$ (JD)" the FloatToString function in the generated code?

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by JonnyW »

Hi. Very possibly - may well be that the conversion is using 16-bit values internally instead of 32-bit values.

For information, 2456657 as a 16 bit value is 31825.

We can look into the conversion here. In the meantime, if you cast your final Julian Date to a temporary integer (long / 32-bit) and convert that to a string, hopefully that should work OK for you, so:

JDText = string(signed(JD))

Or

TempLong = JD
JDText = string(TempLong)

Cheers,

Jonny

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by MJU »

Hey Jonny,

Thanks for your advice, I still can't show the float as a 32bit integer.
I've made an 32bit signed (LONG) integer: TempLong

Tried:
TempLong = JD
JDText = STRING (TempLong)


This return following error when compiling:
C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\EFEMER~1>"C:\PROGRA~1\Flowcode\v6\COMPIL~1\avr\BATCHF~1\..\bin\avr-gcc.exe" -mmcu=atmega324p -Os -funsigned-char -o "C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .elf" "C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .c" -lm
C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .c: In function 'main':
C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .c:636: error: expected declaration specifiers or '...' before '(' token
C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .c:636: error: expected declaration specifiers or '...' before 'FCV_JDTEXT'
C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .c:636: error: expected declaration specifiers or '...' before numeric constant
C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .c:636: error: conflicting types for 'FCI_TOSTRS32'
C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .c:636: note: an argument type that has a default promotion can't match an empty parameter name list declaration
C:\Program Files\Flowcode\v6\CAL\/AVR\AVR_CAL_String.c:265: error: previous definition of 'FCI_TOSTRS32' was here
C:\-MAP~1\FLOWCO~1\FLOWCO~3\AVR\blabla .c:640: warning: passing argument 1 of 'FCD_04071_LCD1__PrintString' discards qualifiers from pointer target type

Error returned from [avr-gcc.exe]
Completed compilation, return = 1


Seems there is a problem with the "XX=String(YY)" command.
The compiler doesn't return the error when using "FloatToString$" function, but when I use this, it returns the 31825 value on the LCD..

User avatar
JonnyW
Posts: 1230
Joined: Fri Oct 29, 2010 9:13 am
Location: Matrix Multimedia Ltd
Has thanked: 63 times
Been thanked: 290 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by JonnyW »

Hi. OK, I will raise this as a bug (or 2 bugs) and we can look into it.

Cheers,

Jonny

MJU
Posts: 502
Joined: Wed Nov 07, 2007 6:51 pm
Location: Antwerp Belgium
Has thanked: 121 times
Been thanked: 108 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by MJU »

Thanks Jonny..

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Whats wrong with this calculation/simulation?

Post by LeighM »

Fixed file attached for the compiler error
Please replace the installed version in \Program Files (x86)\Flowcode 6\CAL\AVR
Regards,
Attachments
AVR_CAL_String.c
(22.73 KiB) Downloaded 221 times

Post Reply