A well learned individual knows themselves and how to collect and use the tool of knowledge. - techhelpbb [more]
 Chief Delphi Decimal precision in consts and use of double vs float
 portal register members calendar search Today's Posts Mark Forums Read FAQ rules

#1
07-04-2018, 03:09 PM
 Foster Engineering Program Management VRC #8081 (STEMRobotics) Team Role: Mentor Join Date: Jul 2007 Rookie Year: 2005 Location: Delaware Posts: 1,571
Decimal precision in consts and use of double vs float

One of my hot summer day activities in Hellaware, sorry Delaware is to read robot code. I've come across a few things that I've questioned. So I'm asking the developers to answer two simple questions

Why all of the highly precise constant defines?

Why all the doubles?

Constants

We all know that it's faster to use things like the square root of 2 as constants rather than calling sqrt(2) lots of time.

Which leads to seeing things like this in the code

Code:
```#define SQRT_2 1.414213562373095048801688724209698078569671875376948073176679737990733
#define PI 3.14159265358979323846264338327950288419716939937510582097494459
//Efficiency or something```
Lets take the Pi example. We all know that the diameter of a wheel times Pi gives you the circumference. An 8” wheel has the following circumferences depending on what value of Pi you use.

24” where Pi =3
24.8” where Pi = 3.1
25.12” where Pi is 3.14
25.13” where Pi is 3.1416

Assume we want to run the length of the FRC field (55') and assume a 4' long robot from the wall, the front wheel needs to move 51'.

24” where Pi =3 needs to do 25.5 rotations
24.8” where Pi = 3.1 needs to do 24.7 rotations
25.12” where Pi is 3.14 needs to do 24.4 rotations (*)
25.13” where Pi is 3.1416 needs to do 24.25 rotations

Using Pi=3 vs Pi as 3.1416 is a difference of 1.25 rotations, over 2', but the difference in Pi at 2 decimal places 3.14 vs 3.1416 is 0.15 a rotation or about 3 ½ inches of actual travel after going 55 feet.

Six decimal places (3.141593 reduces the error to about 1/2”. Given friction, battery, carpets, windspeed, etc that is pretty amazing.

Key point is that is at 6 decimal places. Using more that that isn't really helping that much, you are slowly closing in to the final point, but you are pushing the ability to make it any better.

So my first question is why the high decimal digit constants? This isn't a space shot (and BTW they make tiny course corrections as they go). Are you really doing something with those extra digits? Or are you just fooling yourself that 3.141592653589793238462643383279 is way better than 3.141593?

Which brings on my second question, why all the doubles? A float on an ARM processor has about 7 decimal digits of accuracy. Unless you are doing a huge number (ie 10K+) of calculations, the rounding error isn't going to be a problem. On the other hand if you are trying to do real time calculations, the cost of doubles in every calculation can be a burden.

While the sages on Stack Overflow say “Meh, just use double”, my second question is “Why all the calculations as double?”.

Thanks for your time on this hot summer day.

(*) On a 12' VRC or 8' VIQ field and 4” stock wheels, Pi = 3.14 is more than enough for the distances they are moving.)
__________________
Foster - VEX Delaware - 17 teams 2016/17 -> 62 teams 2017/18 -> Now: 66 Organizations & 172 teams -- Chief Roboteer STEMRobotics.org
2010 - Mentor of the Year - VEX Clean Sweep World Championship
2006-2019, over a decade of doing VEX, time really flies while having fun
Delmarva Robotics and VEXMen Team Site come see what we can do for you.
#2
07-04-2018, 03:29 PM
 GeeTwo Somebody Else AKA: Gus Michel II no team Team Role: Mentor Join Date: Jan 2014 Rookie Year: 2013 Location: Slidell, LA Posts: 6,040
Re: Decimal precision in consts and use of double vs float

With you all the way. When needed, I usually define pi as 4 * atan(1), but 22/7 is good to almost 1 part in 2500, and 355 / 113 is good to better than one part in 11,000. I once wrote an integer math routine to calculate atan2() in integer degrees based on integer inputs up to 1024, and it was really rather simple to get it right. Unless you're calculating orbits until the next millennium or measuring the mass of a neutrino, there's no need for THAT many digits.

Edit: In response to Brennon,
Start giving away points in anything and you'll find out some day that you've given away too many. My integer atan2() was for an arduino, not a RIO, and I certainly don't mean to advocate for writing such a routine for the RIO! That said, I can't think of any reason to do significant amounts of double precision arithmetic on board an FRC robot - even (or especially) a 900 robot.

While on the subject, I'd like to call out Java* programmers who create a new object when the proper solution is to modify an existing one. Just because you can do something doesn't mean you should.

* there are probably also some C++ programmers who do this, but I've mostly seen it in Java.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
[Quoting brennonbrimhall]: We design a new robot every year, but we can't forget that we also design a new team every year as folks come and go.

Last edited by GeeTwo : 07-04-2018 at 04:42 PM.
#3
07-04-2018, 03:58 PM
 brennonbrimhall Lead Mentor AKA: Brennon Brimhall FRC #6844 (Provotypes) Join Date: Jan 2012 Rookie Year: 2012 Location: Provo, UT Posts: 399
Re: Decimal precision in consts and use of double vs float

My response would be “why not use a double?” In the context of FRC, I haven’t yet been in a bind where I couldn’t afford the memory. Further, Java defaults to using doubles for generic decimals anyway - using floats requires lots of casting. It keeps the code cleaner.

I noticed that you’re part of VRC - I can’t speak to that. It may be that a situation with much more restrictive memory constraints and not using Java may result in floats being more useful. If you’re going to go down this rabbit-hole further, I’d also suggest investigating fixed-point representations; they aren’t common except in embedded applications.
__________________
Code and FRC, my personal blog.

Team 20, 2012-2014: 4 blue banners, 5 medals, and 9 team awards.
Missionary, Church of Jesus Christ of Latter-day Saints, 2014-2016: Colorado Denver South Mission.
Brigham Young University, 2016-present: Computer Science.
Team 6844, 2018-present: 2018 Utah RAS, 2018 Newton/Carver RHS.
#4
07-04-2018, 04:45 PM
 Caleb Sykes Knock-off Dr. Strange AKA: inkling16 no team (The Piztons) Join Date: Feb 2011 Rookie Year: 2009 Location: Minneapolis, Minnesota Posts: 1,771
Re: Decimal precision in consts and use of double vs float

I use the digits of pi past the fourth as a kind of random number generator when I need to come up with a random number in my head. Aside from something like that though, no, later digits don't add much value.
#5
07-04-2018, 05:02 PM
 Foster Engineering Program Management VRC #8081 (STEMRobotics) Team Role: Mentor Join Date: Jul 2007 Rookie Year: 2005 Location: Delaware Posts: 1,571
Re: Decimal precision in consts and use of double vs float

I've seen the ue of doubles in lots of C++ code, and it's been mostly C++ code I've been browsing recently.

Java has some other issues with math, a little Google will get you case of where the JVM doesn't make good choices. VRC control systems are pretty grunty for the size of the robot, the CPU in the VIQ system is pretty spiffy. But we put motor speeds (-127 -> 127) into ints rather than doubles.

I'm also a programmer of the GeeTwo base, I've done robotic stuff on Z80's (lets build a sin table and do lookups rather than calculate). So I'm always looking at wasted cycles. In general I see programmers just doing things without looking at the overall effect. Which is why I need a 16GB of memory and 1Gb internet links to do simple things. As an example, CD, small footprint, loads in a heartbeat, bandwidth doesn't eat my data plan for lunch. I have an ancient Wordpress site, it's the same way. The new Wordpresss wallows like a hog waiting to be made into IRI cutlets.

I liked GeeTwo's comment, don't give points away. I'd rather spend the cycle on vision or making sure I have PID running where possible.
__________________
Foster - VEX Delaware - 17 teams 2016/17 -> 62 teams 2017/18 -> Now: 66 Organizations & 172 teams -- Chief Roboteer STEMRobotics.org
2010 - Mentor of the Year - VEX Clean Sweep World Championship
2006-2019, over a decade of doing VEX, time really flies while having fun
Delmarva Robotics and VEXMen Team Site come see what we can do for you.
#6
07-04-2018, 05:41 PM
 GeeTwo Somebody Else AKA: Gus Michel II no team Team Role: Mentor Join Date: Jan 2014 Rookie Year: 2013 Location: Slidell, LA Posts: 6,040
Re: Decimal precision in consts and use of double vs float

Here's another real-world lesson in precision, from my career, and currently an issue. My work group is responsible for running performance predictions of US NAVY sensors over broad areas, interpreting these, and sending them forward to planners and operators. The predictions are run at double precision, but no one even pretends to trust these predictions to a resolution any finer than 0.1 dB, which is to say, about 2.3%, or about 5.4 binary digits of mantissa. About 15 years ago, "Jim" wrote some code to convert double precision binary quadrature code into single precision amplitudes. He intended it to last a year or so. We're running that code to this day, because it reduces the data we need to send over the network by a factor of at least four, and the reduced data is worth as much as the original.
To intentionally misquote St Bernard of Clairvaux:
Quote:
 The road to hell is paved with unnecessary precision.
__________________

If you can't find time to do it right, how are you going to find time to do it over?
If you don't pass it on, it never happened.
Robots are great, but inspiration is the reason we're here.
Friends don't let friends use master links.
[Quoting brennonbrimhall]: We design a new robot every year, but we can't forget that we also design a new team every year as folks come and go.

Last edited by GeeTwo : 07-04-2018 at 05:44 PM.
#7
07-04-2018, 05:55 PM
 nickbrickmaster Registered User AKA: Nick Schatz no team ('Snow Problem, 3184 Alum) Team Role: Programmer Join Date: Jan 2015 Rookie Year: 2014 Location: Eagan MN Posts: 476
Re: Decimal precision in consts and use of double vs float

I agree that defining Pi past the 5/6th decimal place is totally unnecessary. You run into sensor limits far before software precision limits.

However, the tiny pure math part of me says "get everything as exact as possible". That's why I tried to work out the exact Hessian for the distance from a spline for about fifteen minutes, before I gave up and wrote a function that approximates it. I indulge this tiny math demon on my shoulder by using doubles and writing math.pi instead of 22/7.

I think using floats instead of doubles on the RIO is premature optimization. That's a drop in the bucket compared to everything else going on. If it takes less than 20 ms to complete a robot loop, you're letting that time go to waste anyway. If you're taking more than 20 ms to complete a loop, then you need to first stop using Python, second, start thinking about optimizing hardware calls and etc, about thirty more items, then convert all your doubles to floats.

I can see the reason on Arduino and other embedded processors. I've run out of RAM on an Arduino before.
__________________
This is a postmodern signature.
#8
07-04-2018, 06:30 PM
 calcmogul WPILib Developer AKA: Tyler Veness FRC #3512 (Spartatroniks) Team Role: Mentor Join Date: Nov 2011 Rookie Year: 2012 Location: Hawthorne, CA Posts: 109
Re: Decimal precision in consts and use of double vs float

Regarding the extra precision in the original post, floats have about 7 digits of precision and doubles have about 15 digits of precision. If you're using constexpr floats or doubles for arithmetic anyway, you might as well specify as many digits as can be represented in the type. Using a worse approximation like 22/7 in that representation is leaving free precision on the table.

Quote:
 Originally Posted by nickbrickmaster I think using floats instead of doubles on the RIO is premature optimization. That's a drop in the bucket compared to everything else going on. If it takes less than 20 ms to complete a robot loop, you're letting that time go to waste anyway. If you're taking more than 20 ms to complete a loop, then you need to first stop using Python, second, start thinking about optimizing hardware calls and etc, about thirty more items, then convert all your doubles to floats.
Using floats instead is a premature optimization in one other way. When we transitioned WPILib's C++ impl from using floats to doubles a year or two ago, we measured the time taken on a small floating-point benchmark on a roboRIO. When compiling with -Og (basically -O1 with some optimizations disabled for debuggability), the difference between floats and doubles was negligible.

We've tested other things as well in C++ like whether std::clamp() is better than writing out the if branches manually for clamping the integral term in PIDController.cpp. godbolt.org showed little difference in ARM assembly output for GCC 5.4 (the std::clamp() version was one fewer instruction iirc, but that's probably just noise). Compilers are pretty smart nowadays, so write for readability and maintainability first, then optimize where you measure performance problems.
__________________

Last edited by calcmogul : 07-04-2018 at 06:34 PM.
#9
07-04-2018, 06:54 PM
 Michael Hill Registered User FRC #3138 (Innovators Robotics) Team Role: Mentor Join Date: Jul 2004 Rookie Year: 2003 Location: Dayton, OH Posts: 1,892
Re: Decimal precision in consts and use of double vs float

One place it really pays off to use floats rather than doubles is when your data is being transmitted wirelessly and you are really crunched for bandwidth where it doesn’t really matter if your values are off in the 10th decimal. This can happen quite readily in aerospace applications (among others). That said, I don’t think many teams are coming close to hitting their bandwidth limit unless they’re not being at all careful with video streams, so justifying floats over doubles in FRC seems rather difficult.
__________________
'12-'xx: Mentor FRC 3138 - Innovators Robotics
'07-'07: Mentor FRC 1646 - Precision Guessworks
'04-'06: Student FRC 45 - TechnoKats
#10
07-04-2018, 09:35 PM
 Thad House Volunteer, WPILib Contributor no team (Waiting for 2021) Team Role: Mentor Join Date: Feb 2011 Rookie Year: 2010 Location: Thousand Oaks, California Posts: 1,256
Re: Decimal precision in consts and use of double vs float

Another reason we chose doubles for wpilib, especially in the Java API is that constants in Java default to doubles, and don't inplicitly get converted to floats. Also all the operations are only defined on doubles. So the amount of required casting was kind of insane. And as posted above, the speed performance was negliable.
__________________
All statements made are my own and not the feelings of any of my affiliated teams.
Teams 1510 and 2898 - Student 2010-2012
Team 4488 - Mentor 2013-2016
Co-developer of RobotDotNet, a .NET port of the WPILib.
#11
07-04-2018, 11:19 PM
 Prateek M Kotlin Master Race FRC #5190 (Green Hope Falcons) Team Role: Programmer Join Date: May 2018 Rookie Year: 2018 Location: Cary, North Carolina Posts: 51
Re: Decimal precision in consts and use of double vs float

Quote:
 Originally Posted by Thad House Also all the operations are only defined on doubles. So the amount of required casting was kind of insane. And as posted above, the speed performance was negliable.
Especially in a language like Kotlin (which we use), everything needs to be casted. Even passing 0 instead of 0.0 to an argument that requires a double would cause a compile-time error. Therefore, it's a matter of convenience for us. And as iterated above, the performance difference is negligible.
#12
07-05-2018, 12:46 AM
 Jared Russell 4933T15 FRC #0254 (The Cheesy Poofs), FRC #0341 (Miss Daisy) Team Role: Engineer Join Date: Nov 2002 Rookie Year: 2001 Location: San Francisco, CA Posts: 3,564
Re: Decimal precision in consts and use of double vs float

Obligatory https://docs.oracle.com/cd/E19957-01..._goldberg.html

TL;DR, it's complicated. Unintuitively, there are real cases where doubles are faster than floats, and cases where floats have less error than doubles. Moreover, seemingly academic differences in precision can compound depending on what sequence of operations you enact on your values.

If it matters, you need to do some careful consideration of how your compiler/processor implements floating point operations. If it doesn't matter, it doesn't matter.
#13
07-05-2018, 07:07 AM
 gerthworm Making the 1's and 0's FRC #1736 (Robot Casserole) Team Role: Mentor Join Date: Jan 2015 Rookie Year: 2015 Location: Peoria, IL Posts: 744
Re: Decimal precision in consts and use of double vs float

OP, awesome and thought-provoking question!

Quote:
 Originally Posted by GeeTwo To intentionally misquote St Bernard of Clairvaux:
In my mind this rings true for the following reason:

--When you have no processing time or memory constraints, you will throw caution to the wind and not bother with determining how much precision is actually needed.
--When you hit processing and memory constraints, they are often sudden and.... unexpected.... leading to frantic slashing to see where you can make gains. Down-sizing variables while still guaranteeing you haven't hosed functionality is an extremely frustrating endeavor.

Unless you have existing, good, 100% range coverage unit tests and can run stuff back to back, this will be a mind numbing endeavor.

If I were to see constants like that defined in code during a review, I would question why the "roll your own" approach was taken, rather than using some sort of built-in math library for the processor/language combo in question.
#14
07-05-2018, 03:59 PM
 notmattlythgoe Flywheel Police AKA: Matthew Lythgoe FRC #2363 (Triple Helix) Team Role: Mentor Join Date: Feb 2010 Rookie Year: 2009 Location: Newport News, VA Posts: 2,387
Re: Decimal precision in consts and use of double vs float

Quote:
 Originally Posted by GeeTwo While on the subject, I'd like to call out Java* programmers who create a new object when the proper solution is to modify an existing one. Just because you can do something doesn't mean you should.
In many cases it is simpler and much more readable to create a new object instead of trying to keep track of what data in an object needs overwritten and deleted so it can be reused.

If you don't NEED to do it, DON'T. Simple code is less buggy, easier to read, and easier to maintain. Just because you CAN reuse an object doesn't mean you SHOULD.

Last edited by notmattlythgoe : 07-05-2018 at 04:11 PM.
#15
07-05-2018, 05:50 PM
 KJaget Zebravision Labs AKA: Kevin FRC #0900 Team Role: Mentor Join Date: Dec 2014 Rookie Year: 2015 Location: Raleigh, NC Posts: 157
Re: Decimal precision in consts and use of double vs float

Quote:
 Originally Posted by Foster We all know that it's faster to use things like the square root of 2 as constants rather than calling sqrt(2) lots of time.
FYI, on the theme of premature optimization, modern C++ compilers will do this optimization this for you.

They won't know pi until you #include <cmath>, and at that point it is named M_PI. No real need to define either in robot code. For C++ anyway, not sure about java. Edit - for some dialects of C++, that is. Pretty sure GCC provides them in most cases, but your mileage may vary.

The number of digits might be for historical reasons - some systems had 80 bit or longer floating point registers (e.g. https://en.wikipedia.org/wiki/X87).

But yeah, unless you know what you're doing float vs. double won't make a difference. But if you do, there is potential, e.g. https://gist.github.com/tetsu-koba/5873935. This would make a lot more sense for, say, image processing instead of control code, but maybe there's a chance it might do something if you're lucky on other code

Last edited by KJaget : 07-05-2018 at 05:56 PM.

 Thread Tools Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home Announcements     User Announcements FIRST     General Forum     Rumor Mill     Career     Robot Showcase Technical     Technical Discussion     Robotics Education and Curriculum     Motors     Electrical         CAN     Programming         NI LabVIEW         C/C++         Java         Python     Sensors     Control System     Pneumatics     Kit & Additional Hardware     CAD         Inventor         SolidWorks         Creo     IT / Communications         3D Animation and Competition         Website Design/Showcase         Videography and Photography         Computer Graphics Competition     Awards         Chairman's Award     Rules/Strategy         Scouting     Team Organization         Fundraising         Starting New Teams         Finding A Team         College Teams     Championship Event     Regional Competitions     District Events     Off-Season Events     Thanks and/or Congrats     FRC Game Design     OCCRA         OCCRA Q&A Other     Chit-Chat         Games/Trivia         Fantasy FIRST     Car Nack's Corner     College & University Education     Dean Kamen's Inventions     FIRST-related Organizations         The Blue Alliance     FIRST In the News...     FIRST Lego League     FIRST Tech Challenge     VEX     Televised Robotics     Math and Science     Robot in 3 Days (RI3D)     NASA Discussion ChiefDelphi.com Website     CD Forum Support     Extra Discussion

All times are GMT -5. The time now is 03:53 PM.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.

 -- English (12 hour) -- English (24 hour) Contact Us - Chief Delphi - Rules - Archive - Top