printing FLOAT data type on the terminal window

I have a question -
can i print float variable on the terminal window?
for example:


float a=2.555;
a*=4.5;
printf(“a = %f”, a);

does the RC prints float vriables on the IFILoader’s Terminal Window?

Last I checked, the printf in PIC support we use did not support floats. Floats, and doubles, are in general a bad idea on this processor. I reccomend fixed point, or aritimetic using rational numbers. See http://srvhsrobotics.org/eugenebrooks/IntroCProg.pdf There is a routine described in this document, and available for download, that prints fixed point numbers.

Eugene

I don’t think it’s that bad to use a couple floats, but overuse can lead to slow downs in processing time.
the use of floating point numbers is not native to the PIC, Instead MPLAB uses a software routine to do the math (this is done at compile time), internally everything is probably represented differently. So everytime you do a float there extra steps the processor has to take.

If you want to printf it you could just multiply by 100 or 1000 or however much accuracy you want and then cast it to an int.

The RC (actually, the microcontroller) does not have hardware support for floats. It supports floats software-side, but it is very slow.

I looked at the source for the libraries last year and float data type is not included in the vfprinf routine, the code behind printf. Your best bet is to multiply your float by 100 or 1000 depending on how many deicmals you want put the result in a long and printf the result. Aviod floats whenever possible!

If enough people want to be able to print floats, I’ll write code to do it (by decomposing the float into sign, exponent, mantissa). It’ll take a couple hours.

Let me know…I’m always looking for something to do.

i asked that because we have a function to calculate the range between the robot and the Rack, and it depends the TILT angle of the camera.
the TILT ANGLE is being used as an integer, so if the angle is 8, then the distance is 9 meters. if the angle is 7, than the distance is 6 meters. i want something in the middle like 6.5 angle and so on, to be more accurate.
but thanks ill use something i saw here, you can see this thread as closed… thanks

if you have a smart way to prints floats, so it will be very usefull here i think. but i saw a reply in this thread which says to print it like " %d.%d ", but if you have another way… it will be usefull, thanks.

The prinf function isn’t capable of printing floats. Instead convert your float to an integer and multiple it by 100.0. Use %d and print it. That should work.

float tan = -12.423421;
printf("Tan is = %d
",(int)(tan * 100.0));

Floats are bad to use. They are bad because sometimes you get ridiculous numbers, for instance

23.98071849629364097134098729873403214

or larger. It runs slowly because it takes up extra memory, and there is a limited amount of memory in the controller.

Good luck

Floats aren’t necessarily bad if used in moderation. They don’t take any more data memory than a long does as both float and double are 32 bits long. Floating point operations are emulated in software since there is no on-board FPU hardware. The math library linked in uses a modest amount of additional program memory - but there is a lot of program memory available. It doesn’t cost any more program memory to do 1 or 1000 float operations once the library is linked in.

What a programmer needs to be aware of is that the software emulation of the floating point operations can take a lot of time. How much time? I wrote a program to test how many floats/second the library could support and it wasn’t much… maybe a few 1000s/second and you’d be saturating the PIC. I’ll have to go back and re-run the benchmark to remember exactly.

The float/double data type is 8 bits of signed exponent and 24 bits of data - so a 32 bit integer value actually has 8 bits more precision than a float.

As with anything, understanding its weaknesses as well as its strengths will allow you to make a well informed decision based upon tradeoffs. Personally, I try to avoid both longs and floats but sometimes it just makes more sense to use them vs anything else.

What do you mean by ‘more precision’ in this case?

Thanks,
Robinson

For the same exponent, which is what you’ll get when you align float exponents for math operations, you only have 24 bits to represent your number. Longs are 32 bits. Using fixed point arithematic with longs is more precise than using floats as longs have 8 more bits of data than floats.

Just a quick interjection if you use Floats or Doubles dont forget to format your output with %.Xf. The X is what place you want to format after the decimal. like %.2f outputs to the thousands place || x.00. If the decimal is not a double watch out for grabage data allways do some form of formating on the %F output because on a system there is a max and min a float can be, after that the system literaly gives you garbage and if your working on something that has to be exact then dont forget, in also to that comment if you want things exact then use intergers computers like base 2 and 1 && 0 so if you want to drive the robot 2.3 try to rework your measures 1 place value and move the values down a place value. have a great day!