Multiply integer with float?

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
jadiflow
Flowcode v5 User
Posts: 257
Joined: Thu Apr 17, 2008 9:59 am
Contact:

Multiply integer with float?

Post by jadiflow »

Hi all, question: if I multiply an integer with a floating point number, is the result a float? Will it work correctly?

Jan Didden
Linear Audio

mnf
Valued Contributor
Valued Contributor
Posts: 924
Joined: Wed May 31, 2017 11:57 am
Contact:

Re: Multiply integer with float?

Post by mnf »

Things should work as you expect,, with FC and the C compiler taking care of type conversions behind the scenes.
If using a constant then use 1.0 (etc) rather than 1 to avoid conversion.

Sometimes need a bit of care for example float = byte * byte - the conversion would take place after the multiplication and if it overflows 8 bits would be wrong..

Martin

jadiflow
Flowcode v5 User
Posts: 257
Joined: Thu Apr 17, 2008 9:59 am
Contact:

Re: Multiply integer with float?

Post by jadiflow »

Thanks Martin, that is helpful.

Jan Didden
Linear Audio

User avatar
Benj
Matrix Staff
Posts: 15009
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: Multiply integer with float?

Post by Benj »

In Flowcode 7 and 8 you can typecast an int as a float for doing calculations using the keyword FLOAT.

For example.

floatvar = FLOAT intvar * floatvar2

Post Reply