Hot answers tagged

22 votes
Accepted

How do I convert a float into char*?

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 ...
user avatar
  • 1,795
12 votes
Accepted

Only 2 decimal places in printed float

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 ...
user avatar
10 votes
Accepted

Serial monitor can't display float and double values with absolute greater than 4294967040=2^32-2^8

It's not the data type, it's the printing routine that sets that limit. In Print.cpp: 227 if (isnan(number)) return print("nan"); 228 if (isinf(number)) return print("inf"); ...
user avatar
  • 103k
9 votes

Only 2 decimal places in printed float

By default, Serial.print() prints floats with two decimal digits. float num = 7.875; Serial.println(num, 4); will print num with 4 decimal digits, thus: 7.8750. The precision of float is not ...
user avatar
  • 375
8 votes
Accepted

Is there a non-float alternative to pow()?

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 ...
user avatar
8 votes
Accepted

Will an ATmega2560 board be faster than a Uno for hard real-time data logging?

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 ...
user avatar
  • 103k
8 votes
Accepted

Print with round towards zero instead of round to nearest

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 * ...
user avatar
6 votes

Converting from Float to Int yielding weird results. What am I doing wrong?

On an Arduino board with the 328 processor, an INT is stored as a 2 byte value, so the max number is 32,767. You want to use a LONG which is a 4 byte number which has a max of 2,147,483,647. When the ...
user avatar
  • 614
5 votes
Accepted

How to append float value of into a string ?

This has been answered in the previous question but I can repeat it here: void loop() { ... float latitude = 33.546600; float longitude = 75.456912; String buf; buf += F("your location is \...
user avatar
  • 7,851
5 votes
Accepted

float precision in arduino

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 ...
user avatar
  • 4,309
5 votes

Will an ATmega2560 board be faster than a Uno for hard real-time data logging?

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 ...
user avatar
  • 3,077
5 votes
Accepted

Convert float to byte, from Arduino to Raspberry Pi i2c

First of all, if you want the Arduino to send data as a slave, it should do so onRequest(), not onReceive(). Thus: void setup() { Wire.begin(0x08); Wire.onRequest(handleRequest); } Next, as ...
user avatar
  • 36.8k
4 votes
Accepted

Float arithmetic vs. int arithmetic - is there any power penalty?

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 ...
user avatar
  • 36.8k
4 votes
Accepted

Store floats in and reading them from flash

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 ...
user avatar
  • 103k
4 votes

When is it necessary to use "float" instead of "int"?

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 ...
user avatar
  • 6,591
4 votes

Floating-point without FPU

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; } ...
user avatar
  • 36.8k
4 votes
Accepted

Make a float using integers

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 ...
user avatar
  • 36.8k
4 votes

All floats feedback a rounded down integer

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; // ...
user avatar
  • 36.8k
4 votes
Accepted

Convert float to int error

1.06 is not a float. The closest float is 1.059999942779541015625. If you multiply that by 100 you get a number that, again, is not a float. Rounding to the nearest float yields 105.99999237060546875, ...
user avatar
  • 36.8k
3 votes
Accepted

How to speed up

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 ...
user avatar
3 votes
Accepted

build fixed size strings from floats

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 ...
user avatar
  • 103k
3 votes
Accepted

Avoiding float math to speed up arduino

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 ...
user avatar
  • 36.8k
3 votes
Accepted

Sending and receiving different types of data via I2C in Arduino

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 ...
user avatar
  • 359
3 votes

float precision in arduino

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
user avatar
3 votes

Assembling a float from an array of bytes

Just cast the pointer. float conAck = *((float*)(bufferBytes + 9));
user avatar
3 votes

When is it necessary to use "float" instead of "int"?

"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 ...
user avatar
  • 6,932
3 votes

Assigning float to 10 decimal place for longitude,latitude

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: ...
user avatar
  • 7,851
3 votes

Will an ATmega2560 board be faster than a Uno for hard real-time data logging?

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 ...
user avatar
  • 1,553
3 votes

Arduino not adding decimals correctly on double variable

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 ...
user avatar
  • 103k
3 votes
Accepted

How to use a coma as the decimal separator?

Print the integer part, print a comma, then print the value minus the integer part. float value = 123.45; int ival = (int)value; int frac = (value - ival) * 100; Serial.print(ival); Serial.print(",")...
user avatar
  • 103k

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