floating point calculation yields strange result, what is wrong?

Started by basiclover, Jan 26, 2022, 01:44 PM

Previous topic - Next topic

basiclover

Flt2 displays as -0.00 on LCD !!??


''****************************************************************
'*  Name    : 16F877 .BAS                                      *
'*  Author  : basiclover                   *
'*  Notice  : Copyright (c) 2022 [select VIEW...EDITOR OPTIONS] *
'*          : All Rights Reserved                               *
'*  Date    : 1/26/2022                                         *
'*  Version : 1                                                 *
'*  Notes   :                                                   *
'*          :                                                   *
'****************************************************************

;-------------------------------------------------------------------------------
;**** 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)
 
 ' Create an operation with floating values for the demo
    Dim Flt1 As Float
    Dim Flt2 As Float
    Flt1=125000000
    Flt2=4294967295/Flt1       
'---------------------------------------------------
' The main program starts here
Main:
    Cls    ' Clear the LCD
    Print At 1,1, Dec Flt2

top204

The values are too large for 32-bit Floating Point.

32-bit Floating Point is meant for smaller values, and for very large values, you will need to move to a PIC24 or dsPIC33 device with 64-bit Floating Point variables named "Double".

To cater for very large values in 32-bit floating point, you can try dividing the values by a divisor of 10, 100, 1000, 10000 etc, then perform the calculations, then see if they will fit in the final floating point variable when multiplied up again.

basiclover

Thanks Les
I thought that it will work because my numbers are in the range stated in the manual

top204

The compilers use the Microchip format for the Floating Point variable with the 8-bit devices. This was because, 18 years ago, I did not quite know how to create the IEEE754 floating point formats, so I foolishly, used the official Microchip method, which I later, many years later, found out to be not as good as the standard IEEE754 format, but it was too late to change all the code within the 8-bit compiler because it would have meant a virtual re-write! I used the values given by Microchip for the max and min of their Floating Point, and it is generally OK, as long as some calculations are not performed on the min and max values. At the time, I did not know how bad Microchip are at "being correct", and trusted their "official" PDS documents for the Formats. :-( I will be removing the values from the manuals, and add a section on how inneficient Floating Point can be and not use it as a general variable type.

Remember, the PIC 8-bit devices are very minamalist, and were never really designed to be miniature computers, hence the name "PIC", which used to stand for "Peripheral Interface Controller" when they were first developed by GE, and were designed as a simple control chip between items. So for Floating Point to be performed on the early 14-bit devices is actually quite remarkable with their few menmonics and small amount of fragmented RAM!

For the 16-bit compiler, I had some time to learn the "real" Floating Point formats, and not use Microchip's format. I use the standard IEEE754 format, so they operate better, on better devices that are "miniature computers". :-)