Convert Integers to Float

# Thread: Convert Integers to Float – 607 days old

1. 0

## Convert Integers to Float

Hi all,
I have searched the manual and I have not found a command that converts integer variables (Whole number & decimals) to a variable Float.
Then I have written a macro for this function. Now I have the doubt how to convert a negative variable to Float?
Any idea?
See the macro.
Syntax: MyResultFloat = IntegerToFloat(WholeWord,DecimalsWord,3) ' 3 decimals
Code:
```' Declare local variables
Dim PP_AARG As Float System  '/ Compiler variable
Dim _iFltLoop As Byte System
Dim _iInpDecimals As Word System
Dim _iInpInteger As Dword System
Dim _inPow10 As Float System
Dim _iFltResult As Float System

\$define IntegerToFloat(pWholeV,pDecimalsV,pNbDecimals) _mIntegerToFloat pWholeV,pDecimalsV,pNbDecimals

_mIntegerToFloat Macro- pWholeV,pDecimalsV,pNbDecimals
#if (Prm_Count != 3)
#error "Incorrect amount of parameters for _mIntegerToFloat macro. 3 Parameters required!"
Exitm
#else
#if (Prm_1 != Dword) && (Prm_1 != Word) && (Prm_1 != Byte)
#error "_mIntegerToFloat - (Param1) Should be a Dword, Word or a Byte!"
Exitm
#else
#if (Prm_1 == Byte)
Byte_Dword pWholeV,_iInpInteger
#endif
#if (Prm_1 == Word)
Word_Dword pWholeV,_iInpInteger
#endif
#if (Prm_1 == Dword)
Dword_Dword pWholeV,_iInpInteger
#endif
#endif
#if (Prm_2 != Dword) && (Prm_2 != Word) && (Prm_2 != Byte)
#error "_mIntegerToFloat - (Param2) Should be a Dword, Word or a Byte!"
Exitm
#else
#if (Prm_2 == Byte)
Byte_Word pDecimalsV,_iInpDecimals
#endif
#if (Prm_2 == Word)
Word_Word pDecimalsV,_iInpDecimals
#endif
#if (Prm_2 == Dword)
Dword_Word pDecimalsV,_iInpDecimals
#endif
#endif
#if (Prm_3 != Num8)
#error "_mIntegerToFloat - (Param3) Should be a Num8!"
Exitm
#else
#if (pNbDecimals == 0)
_iFltLoop = 1
#else
#if (pNbDecimals > 3)
_iFltLoop = 3
#else
Num_Byte pNbDecimals,_iFltLoop
#endif
#endif
#endif
#endif
GoSub _mIntegerToFloat_Sub
#if (_mIntegerToFloat_RETURN != 1)
#error "_mIntegerToFloat - Mandatory return parameter misssing"
Exitm
#else
#if (Return_Type != Float)
#error "_mIntegerToFloat - Return variable should be a Float variable"
Exitm
#else
Float_Float _iFltResult,Return_Var
#endif
#endif
Endm

#ifMacro- _mIntegerToFloat
_mIntegerToFloat_Sub:
_iFltResult = _iInpInteger  '/ Float <= Dword

_inPow10 = 1
Repeat
#ifdef Watchdog_req
Clrwdt
#endif
Dec _iFltLoop
_inPow10 = _inPow10 * 10
PP_AARG = Dig _iInpDecimals,_iFltLoop
PP_AARG = PP_AARG / _inPow10
_iFltResult = _iFltResult + PP_AARG
Until _iFltLoop = 0
Return
#endIfMacro-```
Regards
Alberto

2. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

3. 0

## Re: Convert Integers to Float

I just use
fVar=iVar
It all happens automatically. It may not be in the manual but I use it all the time.
Maybe I didn't understand your question Alberto?
Best wishes
John

4. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

5. 0

## Re: Convert Integers to Float

As John has correctly stated, the transfer of the contents from one type of variable to another is a standard operation done by the compiler. This is normal on all the standard BASIC languages. The compiler transfers the signed or unsigned contents of the operand into the assignment variable.

6. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

7. 0

## Re: Convert Integers to Float

Exactly this works very well. My question was how to pass the variables not signed and signed by the macro. The conversion is for 2 variables: Result & Decimals. I have seen that the variables must to pass as is. I have had to manage the negatives separately. The new macro works very well.
My confusion has been increased by some problems with the compiler, see Anomaly Reports.
Regards
Alberto

8. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

9. 0

## Re: Convert Integers to Float

The problem has been to pass the decimals to a negative float variable. But the problem is solved and the macro works very well.
See the code HERE.

10. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

11. 0

## Re: Convert Integers to Float

I still don't understand what this macro is used for and why it's needed.

Can you give some examples of using it where just assigning a value to a float variable doesn't work?

12. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

13. 0

## Re: Convert Integers to Float

I have to say that I'm a bit confused too. Integers, by definition, are whole numbers. So why not...

Var1(float) = Var2(integer)

Perhaps there's something I'm missing here. Apologies if that's the case.

Bob

14. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

15. 0

## Re: Convert Integers to Float

Hi George & Bob,
George is right, I have to give an example to understand all the possibilities.
After performing some calculations with integer positive variables, I wanted to give the same results in Float format. So I wanted it to be a universal code (macro) transforming the positive or negative variables (Byte/SByte/Word/SWord) + decimals to Float.

Division with positive numbers:
Code:
```    InputWord = 1250
DivisorWord = 145
' Whole number
ResultWord = InputWord / DivisorWord
RemainderWord = InputWord // DivisorWord   '/ => Remainder.
' Decimals
TempDWord = RemainderWord * 1000        '/ For 3 decimals.
DecimalWord = TempDWord / DivisorWord   '/ = 3 decimals.

VarFloat = ResultWord  '/ Pass the whole ineger to Float.
VarFloat = VarFloat + (DecimalWord / 1000) '/ Pass the decimals to Float.```
And the Float result is correct.

Division with negative numbers:
Code:
```    InputSWord = -1250
DivisorWord = 145
' Whole number
ResultSWord = InputSWord / DivisorWord
RemainderWord = InputSWord // DivisorWord   '/ => Remainder.
'/ Decimals
TempDWord = RemainderWord * 1000        '/ For 3 decimals.
DecimalWord = TempDWord / DivisorWord   '/ = 3 decimals.

VarFloat = ResultSWord  '/ Pass the whole negative to Float.
VarFloat = VarFloat + (DecimalWord / 1000) '/ Pass the decimals to Float.```
And the Float result is erroneous.

It is what I have found out and I do not know if it is the right thing to do. So by transforming the negative variables into positive ones and adding the decimals, everything works fine.
If a input variable is negative then at the end it is done:
VarFloat = -VarFloat

Alberto

16. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

17. 0

## Re: Convert Integers to Float

It looks like you're mixed signed and unsigned variables in the 'results'.
It also seems that the '//' remainder operator returns an unsigned result. Don't know if that's intended or not, but you have to correct for that.

Try this:
Code:
```InputSWord = -1250
DivisorWord = 145

' Whole number
ResultSWord = InputSWord / DivisorWord
RemainderSWord = InputSWord // DivisorWord   '/ => Remainder.
'remainder operator doesn't return signed value, so set sign of remainder = sign of result
if ResultSWord < 0 then
RemainderSWord = -RemainderSWord
endif

'/ Decimals
TempSDWord = RemainderSWord * 1000        '/ For 3 decimals.
DecimalSWord = TempSDWord / DivisorWord   '/ = 3 decimals.

varfloat = ResultSWord  '/ Pass the whole negative to Float.
varfloat = varfloat + (DecimalSWord / 1000) '/ Pass the decimals to Float.```

18. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

19. 0

## Re: Convert Integers to Float

Hi George,
It seemed obvious. I have not seen the tree in the forest. So why the Remainder is not negative when the result of the division is negative?
Thank you very much.
Regards
Alberto

20. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

21. 0

## Re: Convert Integers to Float

Alberto,
When finding the remainder of a division the sign of the remainder is the same as that of the dividend. However when finding a modulus the sign is that of the divisor.
So -9 mod 2 gives a value of +1
And -9 mod -2 Should give value of -1.
That's if I understand how it works.
John

22. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

23. 0

## Re: Convert Integers to Float

John,

(1) Dividend(+) / Divisor(+) = Result(+) => Remainder(+), Decimals(+)
(2) Dividend(-) / Divisor(+) = Result(-) => Remainder(+), Decimals(+)
(3) Dividend(+) / Divisor(-) = Result(-) => Remainder(+), Decimals(-)
(4) Dividend(-) / Divisor(-) = Result(+) => Remainder(+), Decimals(-)

- The remainder is always positive.
- Decimals are not always the same sign as the result of division.
Then it is necessary to write a macro that allows to obtain a correct conversion to float variable.
Regards
Alberto

24. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

25. 0

## Re: Convert Integers to Float

Hi Alberto,
My maths is less reliable now.
I'll do some research to get a better answer for you. I'll then do some tests with Proton and Delphi to see if there is agreement between the two languages. I can't program in C so can't check with that.
We should really go back to the standard definition, whatever that is.
John

26. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues

27. 0

## Re: Convert Integers to Float

Hi Alberto,
If I just play around with numbers. For example
-5 ÷ +2 = -2 remainder must be -1 because if you reverse the operation -2 × +2 = -4 -1 =-5 so remainder must be negative.
Keeping in mind that
- × - = + (in words: minus times a minus equals plus)
similarly
-×+ =- ( in words: minus times plus equals minus)
and the same rules apply for division of signs if you work the examples using negative and positive numbers it can be seen that if the dividend is negative and the divisor is negative or positive then the remainder must be negative.
In addition if the dividend is positive then the remainder will always be positive.
Summary, in the case of remainders the sign always follows the sign of the dividend.

When it comes to modulus it seems there is no standard in programming languages. Look at modulus in Wikipedia where there is a list. There is no consistency. Some languages do not allow for the modulus of a decimal number.
I think I might have confused the issue but I don't think there is a one right answer for modulus.
Good luck
John

28. Attention
×

This valuable resource relies upon the very small amount of revenue generated by displaying online advertisements to our visitors.

Note: Some users have reported issues related to ad-blockers rendering parts of this wesite unusable,
where possible we will rectify the issues to enable you to use this resource with adblocking enabled.

If you can, please report issues in the forum area WebSite / Forum Issues