# Tag Info

17

There is a function in the standard Arduino library called dtostrf(). I think of it as "Decimal to String Float". You pass in the float, how wide you want the whole number to be (if it will fit), the number of decimals of precision - and the buffer you want it to fill. Note that! You need to provide the buffer, and you need to be careful to provide more ...

8

For the general case, @dat_ha 's answer is correct, but it is worth noting that you want a very special case... powers of two. Because computers use binary arithmetic, operations involving powers of two often have some shortcuts available. Multiplying a number by a power of two can be accomplished by the left shift operation (<<), which literally ...

8

No, it will not be any faster. The extra instructions are to access the extra flash memory of the 2560. Instructions like EIJMP - Extended indirect jump, EICALL - Extended indirect call, etc. The compiler option -O3 optimises the most, but there are other options you can enable, such as -funroll-loops which will cause major code bloat but speed things up. ...

8

A cheap trick to round a number to one decimal place (in "chop-off mode") is to multiply it by 10, convert it to an integer, and divide by 10.0f again: float myVal = 123.89f; myVal = (long)(myVal * 10) / 10.0f; // = ((long)1238.9) / 10.0f // = 1238 / 10.0f // = 123.8 You can then print this modified number to the serial monitor. This method can also be ...

6

Use the newest Arduino IDE, because some time ago the 'lto' option was added which is a major optimization for both speed and size. It is possible to test a few compiler options with the #pragma. Try for example these at the top of the sketch: #pragma GCC optimize("-O3") #pragma GCC optimize("-ffast-math") The 'fast-math' is dangerous, be careful with it. ...

5

The simplest way to get more decimal places to print is this: Serial.println(val,NUMBER_OF_DECMIAL_PLACES); This will print out any number of digits after the decimal place. Regular print without the number of digits specified will default to 2 decimal places. for example, here is some code and the result that gets printed: float val = 23.459; Serial....

5

It depends on what your processor is doing while it has no real work to do: If you put it to sleep, then yes, using floats will keep the CPU awake for a longer time and burn more energy. If you are instead doing busy-waits (e.g. with delay()) it should make no difference, since the CPU is busy 100% of the time anyway. Update: According to your update, it ...

5

By default, the Serial print function shows only 2 decimals, however I'm sure the float has the full (unrounded) value, even if it does not show it. You can read about it in the official documentation here With the following code you can use more decimals (fragment from the official documentation): -Serial.println(1.23456, 0) gives "1" -Serial.println(1....

5

Seems too simple: float x = n1 + n2 * 0.1; Is there a trick? Edit: The method proposed by Michel Keijzers, namely float x = n1 + n2 / 10.0; (I removed the redundant casts) can be slightly more accurate, but takes longer to compute, because division is significantly slower than multiplication on the Uno. Computing n2/10.0 always yields the correctly ...

4

There is a helper macro in avr-libc that is designed just for this purpose: #define pgm_read_float_near(address_short) __LPM_float((uint16_t)(address_short)) Read a float from the program space with a 16-bit (near) address. Note The address is a byte address. The address is in the program space. Use it like: PROGMEM float pi=3.141592653; // ...

4

I2C is truly a powerful option of Arduino, for too many reasons; yet the amount of tutorials available are not that many and unfortunately are too complicated for the average person. After working on this for 2 days, I think I have a way to transfer pretty much anything between master and slaves and viceversa. Note that I2C does not transfer floats or even ...

4

What you are asking for is not simple, especially since you have this power function that can be hard to approximate. First, you have to figure out the range and the required precision of each of the floating point numbers you are manipulating. You wrote that readSensorData() returns 7 decimals. If by that you mean that you require a relative accuracy of 10−...

4

An integer is a whole or natural number. Computers use integers for counting and comparisons. Computers can count precisely using integers and comparisons of two integers can be absolutely true or false. To make a float or floating-point number, a computer uses two integer numbers. It uses one integer as the mantissa and the other as the exponent. ...

4

As I said in my comment, all float operations are implemented in software. As a simple test, I compiled the following program: volatile float x, y, z; int main(void) { z = x + y; return 0; } The compiler translated the sum operation into a call to __addsf3. This function extends its arguments to flt40_t format (non-standard 40 bits float), calls ...

4

You already know, from previous answers, why the comparison doesn't give you the result you expect: you assumed that any multiple of 0.1 is also a multiple of 0.001, which is true for real numbers but nor for floats. To fix the issue, it has been suggested to you to allow for some fussiness in the comparison. I would suggest a different approach: keep your ...

4

You already got a couple of perfectly good answer. I will nevertheless suggest a slightly different idiom: float voltage = sensorVal * (5.0 / 1024); or, maybe better: const float V_REF = 5.0; // at the top of the sketch float voltage = sensorVal * (V_REF / 1024); The reason this is slightly better than the other options is because division – even ...

3

The simple way would be to format the string with sprintf - however sprintf on the Arduino lacks floating point support (although it is possible to enable it). So if you convert your floating point numbers into strings first (remember to provide room for the NULL terminating character!) you can then use sprintf to format those converted numbers into what ...

3

A technique commonly used in digital signal processing is using fixed-point arithmetic instead of floating point. In fixed-point arithmetic, one calculates using numbers multiplied by some power of two or ten, and in effect mentally positions a binary or decimal point several bits or digits up while calculating. Numerical analysis may be needed to determine ...

3

You have to parse the character array in order to convert it to floats. There is no fast way to do this: since the Arduino has no FPU, anything involving floats is slow. Unless you really have to, I would not recommend slurping the whole file into RAM. You will save memory by parsing the file on the fly. This should be as simple as calling the parseFloat() ...

3

Just cast the pointer. float conAck = *((float*)(bufferBytes + 9));

3

"int" means integer. It's either one whole number (forgive me, mathematicians, for the sloppy definition!) or the next or previous. A "float" is a floating-point number - that is, a number and part of a number. 3 is an int 3.14 is a float If you need to store/represent a value that can be between integers, you could use a float. Floats use more RAM ...

3

But when after setting the Longitude and Latitude to float.When I use this code, I don't get a valid gps data. Some statements in C/C++ are not what they might seem. The value of the statements: Longitude = (sim808.GPSdata.lon, 10 ); Latitude = (sim808.GPSdata.lat, 10 ); Are actually the same as: sim808.GPSdata.lon; Longitude = 10; sim808.GPSdata.lat;...

3

In the platform.txt file found in ~/Arduino15/packages/arduino/hardware/avr/1.6.17 or similar, you can change the three instances of -Os with -Ofast. The size of the code can increase dramatically though.

3

The 8 bit Arduinos don't have precision, or anything even close to precision when it comes to working with decimal values. There is no such thing as double on an 8-bit Arduino - it is only an alias for float. By using double you aren't gaining any more precision than using float. Even double isn't perfectly precise. It's more precise than float (on ...

3

Try doubles instead of floats. Your initial value, 34567891.234 needs 35 bits of precision to keep track of the decimals, but floats only have 24 bits pf precision. So to have enough precision to keep track of the difference between 34567891.234 and 34567891.234+1, you need higher precision storage. void setup() { // put your setup code here, to run ...

3

As already stated in comments and Dave X's answer, floats have a limited resolution. That resolution is given by the constant FLT_EPSILON, which is the difference between 1 and the smallest float larger than 1. On any system conformant to IEEE 754, FLT_EPSILON is 2−23, or about 1.19e-7. The implication is that, within the interval [1, 2], the real ...

3

From your recent edit: Global variables use 1048 bytes (51%) of dynamic memory, leaving 1000 bytes for local variables. Maximum is 2048 bytes. Your goertzel() function defines this float v[N + 2]; where N is 256. This alone takes 258×4 = 1032 bytes of stack and you only have 1000 bytes available. Then, as I suspected, you are ...

3

You have multiple options: According to jsotola's remark: the easiest is to multiply your value by 2, store it, and after reading, divide it by 2. In one byte you can store a value from 0 to 127.5 (255 / 2), or if you use signed from -64.0 to +63.5. Of course you also can use an offset in case you know the value cannot be less than e.g. -20. If you need a ...

3

Your problem is that you are doing integer maths. mr = (130-20)/(1023-205) reduces to: mr = 110/818 Which normally equates to: mr = 0.135 But since all those numbers are integers the result is an integer, and is truncated at the decimal point, which means that mr is assigned: mr = 0 You can force floating point maths by either including a decimal ...

2

Just write the amount of numbers you want to get float here Serial.println(val); after the val. example: Serial.println(val, 5); It will give 5 digits after the decimal point

Only top voted, non community-wiki answers of a minimum length are eligible