Convert Integers to Float


+ Reply to Thread
Results 1 to 14 of 14

Thread: Convert Integers to Float607 days old

  1. #1
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    689
    Thumbs Up
    Received: 92
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default 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:
        '/ Load the whole part.
        _iFltResult = _iInpInteger  '/ Float <= Dword
    
        '/ Load the decimal part.
        _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
    Last edited by AlbertoFS; 1st October 2018 at 08:14.
    73's de Alberto ea3agv

  2. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  3. #2
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,887
    Thumbs Up
    Received: 91
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  5. #3
    Fanatical Contributor top204's Avatar
    Join Date
    Feb 2002
    Posts
    3,592
    Thumbs Up
    Received: 339
    Given: 162
    Total Downloaded
    1.99 GB

    0 Not allowed!

    Default 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.
    Last edited by top204; 1st October 2018 at 13:50.

  6. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  7. #4
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    689
    Thumbs Up
    Received: 92
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default 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.
    Thank you for your comments.
    Regards
    Alberto
    73's de Alberto ea3agv

  8. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  9. #5
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    689
    Thumbs Up
    Received: 92
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default 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.
    73's de Alberto ea3agv

  10. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  11. #6
    Senior Member tumbleweed's Avatar
    Join Date
    May 2011
    Posts
    524
    Thumbs Up
    Received: 60
    Given: 0
    Total Downloaded
    408.14 MB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  13. #7
    Senior Member Mellbreak's Avatar
    Join Date
    Jun 2012
    Posts
    571
    Thumbs Up
    Received: 36
    Given: 27
    Total Downloaded
    4.63 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  15. #8
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    689
    Thumbs Up
    Received: 92
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default 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
    Last edited by AlbertoFS; 5th October 2018 at 09:25.
    73's de Alberto ea3agv

  16. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  17. #9
    Senior Member tumbleweed's Avatar
    Join Date
    May 2011
    Posts
    524
    Thumbs Up
    Received: 60
    Given: 0
    Total Downloaded
    408.14 MB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  19. #10
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    689
    Thumbs Up
    Received: 92
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default 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
    73's de Alberto ea3agv

  20. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  21. #11
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,887
    Thumbs Up
    Received: 91
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  23. #12
    Senior Member AlbertoFS's Avatar
    Join Date
    Apr 2005
    Posts
    689
    Thumbs Up
    Received: 92
    Given: 2
    Total Downloaded
    4.62 GB

    0 Not allowed!

    Default Re: Convert Integers to Float

    John,
    I have made some tests:

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

    There are 2 comments:
    - 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
    73's de Alberto ea3agv

  24. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  25. #13
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,887
    Thumbs Up
    Received: 91
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default 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.

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

  27. #14
    Prolific Poster John Drew's Avatar
    Join Date
    Feb 2002
    Posts
    2,887
    Thumbs Up
    Received: 91
    Given: 34
    Total Downloaded
    4.60 GB

    0 Not allowed!

    Default 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
    Last edited by John Drew; 9th October 2018 at 13:20. Reason: Tried to clarify

  28. Attention

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

    The advertisements we display are relevant to this web site and your browsing history

    Please consider supporting us by disabling your ad blocker.


    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


    Thank you for your attention.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. calculating % with integers
    By Tim in forum Proton Plus Compiler v3
    Replies: 7
    Last Post: 31st May 2015, 15:26
  2. To Float, or not to Float, that is the question?
    By robbed666 in forum Proton Plus Compiler v3
    Replies: 3
    Last Post: 14th June 2011, 03:44
  3. dc to dc convert
    By fredelek59 in forum Proton Plus Compiler v3
    Replies: 5
    Last Post: 9th May 2007, 10:38
  4. Best way to extract Integers in Float variable
    By dolci in forum Proton Plus Compiler v3
    Replies: 8
    Last Post: 18th August 2006, 20:05
  5. how to convert this
    By C_Moore in forum Proton Plus Compiler v3
    Replies: 16
    Last Post: 8th April 2006, 00:47

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts