Is there an Arduino string library?

I have looked over the forum and not found a satisfactory answer as yet.

Basically, I want to know how to read, for example, a decimal number into the Arduino using one of the serial ports. I see that I can read a character at a time and put the characters into an array. So if I input say 0.123 is thee a library that can do this or am I asking to much from the Arduino? For example, is there a such thing as serial.readfloat() that will get and convert a floating point number, and maybe a float to integer function and such?

You got the right start. Read the digit characters into a character array and append a null character ('\0') to the end of it. Now you have a string with your floating point number in it. Now do a little research on the function atof() and dtostrf().

My Arduino Uno calculated 50 factorial, so I think it can handle 0.123 OK.

45! = 119622220865480194561963161495657715064383733760000000000
46! = 5502622159812088949850305428800254892961651752960000000000
47! = 258623241511168180642964355153611979969197632389120000000000
48! = 12413915592536072670862289047373375038521486354677760000000000
49! = 608281864034267560872252163321295376887552831379210240000000000
50! = 30414093201713378043612608166064768844377641568960512000000000000

There is the C standard string library that can handle a lot of this.

@Nick. What's the precision on big numbers?

They have an arbitrary precision, as they are stored as strings. You configure the number of decimal places you want (eg. for calculating sine, pi, etc.) which can be zero.

You can't, of course, have 5000 digits on a Uno as that would exceed available RAM.

avr-libc functions workon the arduino although they are not declared in the reference.

barryjo:
Basically, I want to know how to read, for example, a decimal number into the Arduino using one of the serial ports.

You can read anything from the Serial port what arrives on the Serial ports.

The default library for Arduino as well as for all AVR controllers is the AVR LIBC library.

There is also a default string library available, but some of the string functions as string conversions to and from numbers are not in the string library, but in other libraries of the AVR LIBC.

But "Serial" is a thing of the Arduino system.

barryjo:
I see that I can read a character at a time and put the characters into an array. So if I input say 0.123 is thee a library that can do this or am I asking to much from the Arduino? For example, is there a such thing as serial.readfloat() that will get and convert a floating point number, and maybe a float to integer function and such?

Yes, there is:

But you should think twice before using these Arduino functions: They are "blocking" up to a maximum timeout time (default: 1 second) and therefore you can only create very slow, very blocking, non-interactive and single-task sketches using such functions.

Normally you would avoid those and write your own serial parsing functions, non-blocking and fast operating.

The most digits I could get on an UNO were 1710 digits.

Once I tried to calculate the biggest Fibonacci number on my UNO that I could do.

Finally it was Fibonacci(8183) with 1710 digits, calculated in something like 14 seconds total.

Unfortunately, my sketch can only do the Fibonacci calculation and nothing else. But 1710 significant digits is not so bad for an Arduino UNO, even if it can only add such numbers and nothing else.

Damn! Just the other day I needed 1711 significant digits...so close...

If you want to receive floating point numbers without blocking here is a state machine thatdoes just that.
It is based on my state machine library the state diagram is attached.

#include <SM.h>

SM parse(invalid);//start with assumption of invalid char
int tvalue;//value under conversion
float fract;
unsigned long divisor;
float value;//value of completed conversion
int sign;
boolean complete;//signal completion

void setup(){
  Serial.begin(115200);
}//setup()

void loop(){
  //only run when chars are available
  if(Serial.available()) EXEC(parse);
  if(complete){
    Serial.println(value);
    complete = false;
  }  
}//loop()

State invalid(){
  char ch = Serial.read();
  if((ch >= '0')&&(ch <= '9')){
    tvalue = convert(ch);
    sign = 1;
    parse.Set(integer);
  }
  else if(ch == '-') parse.Set(negative);
}//invalid()

State negative(){
  char ch = Serial.read();
  sign = -1;  
  if((ch >= '0')&&(ch <= '9')){
    tvalue = convert(ch);
    parse.Set(integer);
  }
  else if(ch != '-') parse.Set(invalid);
}//negative()

State integer(){
  divisor = 1;
  fract = 0;
  char ch = Serial.read();
  if((ch >= '0')&&(ch <= '9')) tvalue = tvalue*10+convert(ch);//convert valid
  else if(ch == '.') parse.Set(fraction);
  else{//or termitate
    value = tvalue*sign;
    complete = true;
    parse.Set(invalid);
  }//else
}//integer()

State fraction(){
  int t = convert(Serial.read());
  if(t == -1){
    value = (tvalue+fract/divisor)*sign;
    complete = true;
    parse.Set(invalid);
  }
  else{
    fract = fract*10+t;
    divisor *= 10;
  }  
}//fraction()

int convert(char ch){
  if((ch >= '0')&&(ch <= '9')) return ch-'0';
  else return -1;
}//convert()

parsing decimal number state diagram.png