View unanswered posts | View active topics It is currently Wed Apr 25, 2018 4:15 pm

 Page 1 of 1 [ 3 posts ]
 Print view Previous topic | Next topic
How to calculate with precision on PIC
Author Message
Rookie

Joined: Sat Apr 13, 2013 10:37 pm
Posts: 8
How to calculate with precision on PIC
So, say I have a calculation I want to run on my PIC:
 Code:int a = 33;float b = 18.888889;int c = (int)(a * b);

How would I go about doing this?
PIC doesn't allow floats or for integer values to go over 32,767.

If I want, say, two-decimal precision, I would do this:
 Code:int a = 33;int b = 1889;int c = (a * b) / 100;

But, then the integer value overflows.

Does anyone have a workaround? I only want two decimals.

Sun Apr 14, 2013 6:26 pm
Site Admin

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Re: How to calculate with precision on PIC
There is no way to calculate with precision on the PIC, but you could do some of the work at compile time. If you have something like
 Code:int time = degrees * 15000 / 90;

Why not change that to
 Code:int time = degrees * 167;

You will always lose precision when you're on a platform without floats and longs, it's a limitation that you have to accept and work around. Sometimes you can do this by doing something like:
 Code:int a = 33;int b = 19;  // you're off by 0.5%int c = (a * b) ;

If that's good enough for you, then you've saved yourself a heap of trouble.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Mon Apr 15, 2013 12:47 am
Rookie

Joined: Sat Apr 13, 2013 10:37 pm
Posts: 8
Re: How to calculate with precision on PIC
Thanks, I guess I'll have to upgrade one of these days.

Mon Apr 15, 2013 12:58 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 3 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.