Serial output Float type data

Source: Internet
Author: User
Tags float double

The printf function in the AVR GCC does not support%f output

Note that the float double type data in GCC is treated as single precision (4 bytes)

There are two ways: 1, the floating-point number is decomposed into 4 bytes, sent separately, the receiver and then the 4 bytes merged into the floating-point number

Examples are as follows:

#include <stdio.h>typedef union{floatF; unsignedCharu[4];} Float4byte;intMainvoid) {Float4byte m1,m2; M1.F=-1.2356; m2.u[0]=m1.u[0];//Let's say this is a transfer process.      M2.U[1]=M1.U[1];      M2.U[2]=M1.U[2]; M2.U[3]=M1.U[3];printf"m1=%f", M1.F); printf ("m2=%f", M2.F); return 0;}


Running Result: m1=-1.235600 m2=-1.235600

2, the floating-point conversion string, the word character sent out, the receiver will then combine these characters into a string, if you want to operation and then convert the string into floating point number.

such as string concatenation using the Sprint function

3, the floating-point number amplification *10, *100, *1000, etc., converted into integer

4. sprintf and floating-point support with AVR GCC

sprintf is easy to use, include <stdio.h>. However, if you want to format the floating-point number, you need the following steps, the simple description is: 1. Need and libprintf_flt.a libm.a Link 2. Need to pass link option:-wl,-u,vfprint

Serial output Float type data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.