Hot answers tagged

25 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 ...
John Burger's user avatar
  • 1,875
17 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 ...
Michel Keijzers's 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"); ...
Majenko's user avatar
  • 105k
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 ...
Fauzan's 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 ...
Jason Clark's 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 ...
Majenko's user avatar
  • 105k
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 * ...
Maximilian Gerhardt's 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 ...
Chad G's user avatar
  • 615
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 ...
Jot's user avatar
  • 3,246
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 \...
Mikael Patel's user avatar
  • 7,969
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 ...
Edgar Bonet's user avatar
  • 42.8k
5 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, ...
Edgar Bonet's user avatar
  • 42.8k
4 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 ...
Edgar Bonet's user avatar
  • 42.8k
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 ...
st2000's user avatar
  • 7,315
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; } ...
Edgar Bonet's user avatar
  • 42.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 ...
Edgar Bonet's user avatar
  • 42.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; // ...
Edgar Bonet's user avatar
  • 42.8k
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 ...
dda's user avatar
  • 1,588
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: ...
Mikael Patel's user avatar
  • 7,969
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 ...
jose can u c's user avatar
  • 6,974
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 ...
Majenko's user avatar
  • 105k
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 ...
James Waldby - jwpat7's user avatar
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
Mobashir's user avatar
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 ...
Majenko's user avatar
  • 105k
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(",")...
Majenko's user avatar
  • 105k
3 votes

Is the strange problem I'm having to do with comparing floats?

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 ...
Edgar Bonet's user avatar
  • 42.8k
3 votes

Two floating point values do not get added and printed on ESP-WROOM-32

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 ...
Dave X's user avatar
  • 2,332
3 votes
Accepted

How to write a float value EEPROM

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 (...
Michel Keijzers's user avatar
3 votes
Accepted

Cannot understand why this code work but this not

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 ...
Majenko's user avatar
  • 105k
3 votes
Accepted

Why does the data I send to the server get rounded off and how do I send the entire float instead?

The constructor for the String object from float is this: String(double, unsigned char decimalPlaces=2); So if you don't want to have 2 decimal digits, you have to use String(someFloat, 3). Also you ...
KIIV's user avatar
  • 4,742

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