News:

;) This forum is the property of Proton software developers

Main Menu

fRound doesn't round floating variable to the nearest integer

Started by basiclover, Feb 08, 2022, 01:34 PM

Previous topic - Next topic

basiclover

fRound 2251799.83678479 displays as 23576


''****************************************************************
'*  Name    : 16F877 .BAS                                      *
'*  Author  : basiclover                   *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 2/8/2022                                         *
'*  Version : 1                                              *
'*  Notes   : test fRound with floating value                    *
'*                                                              *
'****************************************************************

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F877A

Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
    Declare Xtal = 20                            ' Tell the compiler what speed the device will be operating at
    Declare Float_Display_Type = Fast           ' Use the faster, more accurate, but larger, floating point display library routine
'
' Setup the alphanumeric LCD
    Declare LCD_Type = 0       'HITACHI HD44780 controller
    Declare LCD_DTPin = PORTB.4  '4 pin config, data on 4 top pins(PortB4 to B7) of port B
    Declare LCD_RSPin = PORTB.3
    Declare LCD_ENPin = PORTB.2
    Declare LCD_Lines = 2
    Declare LCD_DataUs 75     'LCD time to wait beween data sent(microsec)for some older 16x2 44780 compatible displays (default = 50)
'-------------------------------------------------------------------------------------
    Dim MyFloat As Float
    MyFloat=2251799.83678479
    Print At 1,1, Dec fRound(MyFloat)   'display as 23576
   

trastikata


basiclover

thanks, tryed what you said
now it diplays 2251799, but the nearest integer should be 2251800

trastikata

Quote from: basiclover on Feb 08, 2022, 02:34 PMthanks, tryed what you said
now it diplays 2251799, but the nearest integer should be 2251800

Dec0 will truncate the decimal portion. If you want rounding, try this:

Dim MyFloat As Float
Dim dwTemp As Dword
   
MyFloat=2251799.83678479
dwTemp = fRound(MyFloat)
   
Print At 1,1, Dec dwTemp

basiclover

just found that in the manual and tried it while your message arrived
but doesn' work either

trastikata

Then it's a question for Les, maybe there's a problem with the function.

RGV250

Hi,
This works in 18F452, not got 16 series to sim. Create a temp variable before printing.
    Dim MyFloat As Float
    MyFloat=2251799.83678479
    >>>> Dim tempfloat As Float
    >>>> tempfloat = fRound(MyFloat)
    >>>> Print At 1,1, Dec0 tempfloat   

Bob

RGV250

Hi,
Just tried what Trastikata suggested with a Dword and that works as well so it appears to be device specific.

    Dim tempDword As Dword 
    tempDword = fRound(MyFloat)
    Print At 1,1, Dec tempDword     

Bob

Giuseppe MPO

There is no anomaly in the function
I remember that LES already had an answer to such a question, in 8-bit devices the 32-bit FLOATs are not made for too large number.
If you want more precision, use 14 or 16 devices bit in order to have FLOAT at 64bit

trastikata

Quote from: Giuseppe MPO on Feb 08, 2022, 06:04 PMThere is no anomaly in the function
I remember that LES already had an answer to such a question, in 8-bit devices the 32-bit FLOATs are not made for too large number.
If you want more precision, use 14 or 16 devices bit in order to have FLOAT at 64bit

Giuseppe, it's entirely different problem.

basiclover

@ TRASTIKATA
I must have done something wrong when I worked out your suggestion, It works
My intention by displaying the numbers is to know which walue the pic will actually handles
Did some more tests(below)
test 3 gives me confidence that the right values are handled in calculations
Although variants of the Decx modifier in the print command give unexpected results(test2 and 4 show strange leading zeros)

''****************************************************************
'*  Name    : 16F877 .BAS                                      *
'*  Author  : basiclover                   *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 2/8/2022                                         *
'*  Version : 1                                              *
'*  Notes   : test fRound with floating value                    *
'*                                                              *
'****************************************************************

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 16F877A

Config FOSC_HS, WDTE_OFF, PWRTE_OFF, BOREN_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, CP_OFF

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
    Declare Xtal = 20                            ' Tell the compiler what speed the device will be operating at
    Declare Float_Display_Type = Fast           ' Use the faster, more accurate, but larger, floating point display library routine
'
' Setup the alphanumeric LCD
    Declare LCD_Type = 0       'HITACHI HD44780 controller
    Declare LCD_DTPin = PORTB.4  '4 pin config, data on 4 top pins(PortB4 to B7) of port B
    Declare LCD_RSPin = PORTB.3
    Declare LCD_ENPin = PORTB.2
    Declare LCD_Lines = 2
    Declare LCD_DataUs 75     'LCD time to wait beween data sent(microsec)for some older 16x2 44780 compatible displays (default = 50)
'-------------------------------------------------------------------------------------
    'Dim MyFloat As Float         'test 1
    'MyFloat=2251799.83678479
    'Print At 1,1, Dec0 MyFloat   'displays Dec fRound(MyFloat) displays as 23576   'wrong
'-------------------------------------------------------------------------------------
    'Dim MyFloat As Float         'test 2
    'MyFloat=2251799.83678479
    'Dim MyFloatOut as Dword
    'MyFloatOut=fround(MyFloat)
    'Print At 1,1, Dec8 MyFloatOut   'displays as  0225 1800   (strange leading zero
'-------------------------------------------------------------------------------------
    'Dim MyFloat As Float         'test 3
    'MyFloat=2251799.83678479
    'Dim MyFloatOut As Dword
    'MyFloatOut=fRound(MyFloat)
    'Print At 1,1, Dec MyFloatOut   'displays as  2251800    'right
    'Print at 2,1, dec (MyfloatOut*2) 'displays as 4503600  'right
'-------------------------------------------------------------------------------------
    Dim MyFloat As Float         'test 4  check effect of Dec modifier vaariants
    MyFloat=2251799.83678479
    Dim MyFloatOut As Dword
    MyFloatOut=fRound(MyFloat)
    Print At 1,1, Dec8 MyFloatOut   'displays as  02251800   'strange leading zero
    Print At 2,1, Dec8(MyfloatOut*2) 'displays as 04503600   'strange leading zero
               

tumbleweed

QuoteAlthough variants of the Decx modifier in the print command give unexpected results(test2 and 4 show strange leading zeros)

The 'strange leading zeros' are because with the 'Dec8' modifier you're asking it to display 8 chars, so it fills in  the leading digits with '0'

basiclover