Double precision and CPU slowdown.

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
prem111
Posts: 87
Joined: Sun Feb 23, 2020 3:18 pm

Double precision and CPU slowdown.

Post by prem111 » Sun May 24, 2020 9:12 pm

I needed to get the accuracy to 6 decimal numbers, I replaced:

#define MICROPY_FLOAT_IMPL
on
(MICROPY_FLOAT_IMPL_DOUBLE)

and compiled micropython for port esp32.
Everything works.

Code: Select all

>>> 1/3
0.333333333333333
The problem is the load on the processor, the modules load much slower. Is there another way? I only need to get up to 6 decimal numbers, e.g. 80.123456

User avatar
tve
Posts: 214
Joined: Wed Jan 01, 2020 10:12 pm
Location: Santa Barbara, CA
Contact:

Re: Double precision and CPU slowdown.

Post by tve » Mon May 25, 2020 12:59 am

If that's the GPS coord issue, yeah, represent your lats and longs using integers. You can use fixed point arithmetic or you can represent the whole thing in 1000000th degrees, which will use bigints, but that's perhaps still better than having everything use doubles.

User avatar
pythoncoder
Posts: 4575
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Double precision and CPU slowdown.

Post by pythoncoder » Mon May 25, 2020 6:20 am

I don't know your application. Is worldwide coverage needed? If not, you could express lat and long relative to a local datum for calculations, converting to absolute values only for final display.
Peter Hinch

prem111
Posts: 87
Joined: Sun Feb 23, 2020 3:18 pm

Re: Double precision and CPU slowdown.

Post by prem111 » Mon May 25, 2020 6:44 am

Thanks for reply. I am using this module, it needs to get more precision than just 5 decimal digits in dd - decimal degress formatt.

https://github.com/inmcm/micropyGPS/blo ... ropyGPS.py

The problem is that after receiving the result I have to calculate it further, e.g. the distance to another point or the direction between one and the other waypoint

Examples for my further calculation:

Code: Select all

def dist_waypoints(self, lat1, lon1, lat2, lon2):
        """
        Calculate distance betweeen two coordinates.
        """
        try:
            lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
            result = 6371 * (acos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(lon1 - lon2)))
            meters = int(result * 1000.0);
            return meters
        except Exception as e:
            self.exception_save(e)
            sys.exit()
            return 0

    def bearing_dest(self, lat1, lon1, lat2, lon2):
        """
        Calculate bearing destination betweeen two coordinates.
        """

        try:
            theta1 = radians(lat1)
            theta2 = radians(lat2)
            delta1 = radians(lat2-lat1)
            delta2 = radians(lon2-lon1)
            y = sin(delta2) * cos(theta2)
            x = cos(theta1)*sin(theta2) - sin(theta1)*cos(theta2)*cos(delta2)
            brng = atan2(y,x)
            bearing = degrees(brng)
            bearing = (bearing + 360) % 360
            return bearing
        except Exception as e:
            self.exception_save(e)
            return 0

prem111
Posts: 87
Joined: Sun Feb 23, 2020 3:18 pm

Re: Double precision and CPU slowdown.

Post by prem111 » Mon May 25, 2020 12:57 pm

pythoncoder wrote:
Mon May 25, 2020 6:20 am
I don't know your application. Is worldwide coverage needed? If not, you could express lat and long relative to a local datum for calculations, converting to absolute values only for final display.
Can I have an example please? because I don't quite understand. Thanks.

User avatar
pythoncoder
Posts: 4575
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Double precision and CPU slowdown.

Post by pythoncoder » Mon May 25, 2020 4:54 pm

At present you are trying to represent lat and long such that the entire world is represented to high precision.. This requires N significant digits. If locations were constrained to (say) 1/100 of the globe, your N significant digits could record position with 100x the resolution. At risk of stating the obvious, I can measure my house to 1cm precision in fewer significant digits than would be required to measure Russia.

It's possible to restore the full precision for display by separating the integer and fractional parts and putting them into integers. The absolute coordinates of the datum (chosen to have integer coordinates) are added to the integer part. You then display the two integers with a '.' between them so it looks like a float.
Peter Hinch

prem111
Posts: 87
Joined: Sun Feb 23, 2020 3:18 pm

Re: Double precision and CPU slowdown.

Post by prem111 » Mon May 25, 2020 7:45 pm

Ok, but will it allow me to do calculations later like in my functions which I gave above?

scruss
Posts: 16
Joined: Sat Aug 12, 2017 2:27 pm

Re: Double precision and CPU slowdown.

Post by scruss » Mon May 25, 2020 9:42 pm

Assuming a spherical earth isn't tremendously accurate at the best of times: depending on the distances involved you could be 0.5% off the spheroidal distance. Proper spheroidal calculations are way outside what a microcontroller can do. If you're wanting to calculated arbitrary distances over the whole earth, then there aren't many shortcuts you can take.

User avatar
pythoncoder
Posts: 4575
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Double precision and CPU slowdown.

Post by pythoncoder » Tue May 26, 2020 6:01 am

@prem111 It would help if you told us what you are trying to do.

Take an example. Say you were trying to log points on a hike or a mountain bike ride. At the start you might (programmatically) define a datum being a point near the current location whose absolute lat and long were an integer number of seconds. Subsequent points would be recorded as floating point values relative to that datum. You might perform calculations - distance covered, area enclosed, whatever using these relative values. Recorded points could be converted to absolute coordinates for display as I suggested above.

As @scruss says, there are physical limitations to this. I am merely looking at the arithmetic ;)
Peter Hinch

prem111
Posts: 87
Joined: Sun Feb 23, 2020 3:18 pm

Re: Double precision and CPU slowdown.

Post by prem111 » Tue May 26, 2020 7:39 am

I need to read the current point where auto-rc is located. Next, I want to calculate the distance between the car and the previously set waypoint.

Post Reply