Go to Post The heat gun is not an arm rest. Especially when it is on. - Fuego247 [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 11-02-2008, 23:43
windell747 windell747 is offline
Registered User
FRC #2477
Team Role: Mentor
 
Join Date: Jan 2008
Rookie Year: 2001
Location: Hawaii
Posts: 158
windell747 will become famous soon enoughwindell747 will become famous soon enough
problem with printing float

We're having a problem with printing any floats in our program. We are performing a division of an integer casted to a float and another integer. Even if we assign a float varaible a constant 0.12345 it won't print to the screen. Any help would be greatly appreciated! The file is attached.

One thing we have noticed is that our MPLAB compiler is configured for the PIC18F8722 and successfully compiles. However if we configure it to the PIC18F8520, it gives the following error.

MPLINK 3.90, Linker
Copyright (c) 2004 Microchip Technology Inc.
Error - processor types do not agree across all input files.
Errors : 1

BUILD FAILED: Mon Feb 11 18:40:58 2008

We are suspecting that this is the source of the problem.

Thanks,
Windell
#2477
Attached Files
File Type: c custom_routines.c (5.5 KB, 63 views)
  #2   Spotlight this post!  
Unread 12-02-2008, 00:11
Chaos in a Can Chaos in a Can is offline
Joel Spadin
FRC #1736 (Robot Casserole)
Team Role: Programmer
 
Join Date: Mar 2007
Rookie Year: 2007
Location: Peoria, IL
Posts: 58
Chaos in a Can is an unknown quantity at this point
Send a message via MSN to Chaos in a Can
Re: problem with printing float

printf does not support %f.
Something like printf("%ld", (long)(ratio * 1000) ); might work, though you're likely to run into a lot of problems with floats.

You can avoid floats entirely by shifting the number up to store your decimal places, but in an integer type. Then in calculations, shift it back down.

For example:
Code:
long ratio;
ratio = (((long)p1_x)<<8 ) / S_PWM_SWING;

if(ratio>128 && magnitude_sum>MAX_SINGLE_MOT_MAG){
		RIGHT_WHEEL_PWM=NEUTRAL+MAX_SINGLE_MOT_MAG;
		LEFT_WHEEL_PWM=NEUTRAL-(((1<<8 - ratio) * MAX_SINGLE_MOT_MAG)>>8);
    }
Keep in mind that bit shifts only work on integers, so 0.5<<8 actually becomes 0<<8, so instead of 128, you get 0. You'll have to enter any decimal constants yourself.

(In case you don't already know, x<<8 = x*(2^8) and x>>8 = x/(2^8).)
__________________
2006 Xerox Creativity Award
2007 General Motors Industrial Design Award
2008 Judges' Award
2009 Chrysler Team Spirit Award

  #3   Spotlight this post!  
Unread 12-02-2008, 00:27
dcbrown dcbrown is offline
Registered User
AKA: Bud
no team
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Hollis,NH
Posts: 236
dcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud of
Re: problem with printing float

I posted a floating point print routine last year. Available at http://www.chiefdelphi.com/forums/sh...ad.php?t=55105
  #4   Spotlight this post!  
Unread 12-02-2008, 10:23
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,854
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: problem with printing float

Here's an another alternative with just a little manipulation. You can turn something such as this into a utility function.

Code:
#define ACCURACY 1000  //How many decimal places are important to you
float f;
int i, i2;   // Might need to be longs if you have a lot of significant digits
 
f = 245.56;
 
/* Print float value */ 
i = (int) f;
i2 = (int) ((f-i)*ACCURACY);
printf("f = %d.%03d \r", i, i2); //e.g., f = 245.559
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
printing FLOAT data type on the terminal window razer Programming 14 05-02-2007 08:55
Robots that float boy_scout72688 Rules/Strategy 31 15-01-2007 22:18
Float Design i like dirt Technical Discussion 3 13-09-2006 00:21
Float in MPLAB capenga Programming 1 18-02-2005 06:44
Problem with communicating with STAMP through serial port Skabana159 Technical Discussion 2 06-02-2003 21:10


All times are GMT -5. The time now is 19:12.

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


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi