2

(Im new to the arduino side of stackexchange). Im working on a worksheet for to begin with arduino. I have prior experiences working with arduinos, but problem is new for my field of expertise. Any help will be well appreciated (need help pls uwu)

Problem statement:

Problem statement

I tried different methods to obtain those values, but all are to vain.

Methods that I tried so far:

  • using limits.h, floats.h and inbuild variables of those library -- Problem: Im getting wrong minimum and maximum values for float and double: Wrong output Code:
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>

char fltmax[250];
char fltmin[250];
char dblmax[250];
char dblmin[250];

void sint() {
  Serial.print("Range of signed int: ");
  Serial.print(INT_MIN);
  Serial.print(" to ");
  Serial.print(INT_MAX);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(int));
  Serial.println(" bytes");
}
void usint() {
  Serial.print("Range of unsigned int: 0 to ");
  Serial.print(UINT_MAX);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(unsigned int));
  Serial.println(" bytes");
}
void schar() {
  Serial.print("Range of signed char: ");
  Serial.print(SCHAR_MIN);
  Serial.print(" to ");
  Serial.print(SCHAR_MAX);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(signed char));
  Serial.println(" bytes");
}
void uschar() {
  Serial.print("Range of unsigned char: 0 to ");
  Serial.print(UCHAR_MAX);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(unsigned char));
  Serial.println(" bytes");
}
void slong() {
  Serial.print("Range of signed long: ");
  Serial.print(LONG_MIN);
  Serial.print(" to ");
  Serial.print(LONG_MAX);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(signed long));
  Serial.println(" bytes");
}
void uslong() {
  Serial.print("Range of unsigned long: 0 to ");
  Serial.print(ULONG_MAX);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(unsigned long));
  Serial.println(" bytes");
}
void sshort() {
  Serial.print("Range of signed short: ");
  Serial.print(SHRT_MIN);
  Serial.print(" to ");
  Serial.print(SHRT_MAX);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(signed short));
  Serial.println(" bytes");
}
void usshort() {
  Serial.print("Range of unsigned long: 0 to ");
  Serial.print(USHRT_MAX);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(unsigned short));
  Serial.println(" bytes");
}
void flt() {
  Serial.print("Range of float: ");
  dtostrf(FLT_MIN, -1, 50, fltmin);
  Serial.print(fltmin);
  Serial.print(" to ");
  sprintf(fltmax,"%e", FLT_MAX);
  puts(fltmax);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(float));
  Serial.println(" bytes");
}
void dbl() {
  Serial.print("Range of double: ");
  dtostrf(DBL_MIN, -1, 50, dblmin);
  Serial.print(dblmin);
  Serial.print(" to ");
  dtostrf(DBL_MAX, -1, 0, dblmax);
  Serial.print(dblmax);
  Serial.print(", which has a storage size of ");
  Serial.print(sizeof(double));
  Serial.println(" bytes");
}


void setup() {
  Serial.begin(115200);
  sint();
  usint();
  schar();
  uschar();
  slong();
  uslong();
  sshort();
  usshort();
  flt();
  dbl();
}


void loop() {
}

I tried combining both where the test code that I used before actual implementation:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <float.h>
#include <limits.h>



void setup () {
  char str[30];
  Serial.begin(115200);
  snprintf(str, 30, "Value of float max = %e", FLT_MAX);
  /*puts(str);*/
  Serial.print(str);
  return(0);
}
void loop(){
  
}

But puts(str) is not getting printed in the serial monitor. Then It clicked, that I have to use Serial.(code) for it to get printed in serial monitor, but it ended up in Serial.puts(str) code not found. Then I tried just Serial.print(str), but it just outputs Value of float max = ? (Initially, before I wrote this problem here, it output Va only, which i have no idea how it changed, but its still wrong). What do I do now? also, Why cant I use puts to print in serial monitor?

The board im using for this is Arduino UNO (ATmega328P)

1 Answer 1

4

There are two issues here.

One is that the Arduino core uses Serial to handle serial I/O, rather than the traditional libc interface (printf(), puts() and co.). The avr-libc does provide the traditional stdio functions, but these will not work unless you tell them where stdout is, which is a bit complex.

The take away is to just use the methods of Serial for accessing the serial port.

Then there is the question: why did it print “Value of float max = ?”. The reason is that the avr-libc does not provide by default support for formatting floating point numbers with sprintf(). That support is quite expensive in flash space, and it is omitted unless you link with a specific version of the avr-libc. The simple workarounds are:

  • Use Serial.print() to both format and print floating point numbers, which is inconvenient for very small or very large numbers.
  • Use dtostre() or dtostrf() to format them.

In your case, dtostre() is a better fit:

void setup() {
    char val[40];
    Serial.begin(9600);
    Serial.print("Range of float: ");
    dtostre(FLT_MIN, val, 5, 0);
    Serial.print(val);
    Serial.print(" to ");
    dtostre(FLT_MAX, val, 5, 0);
    Serial.print(val);
    Serial.println();
}

void loop(){}

prints:

Range of float: 1.17549e-38 to 3.40282e+38
1
  • Ahh I see, I'll read up the documentation of dtostre(), my guess its similar to dtostrf(), but the output is rather %e instead of %f, but I'll read just in case. Also, I just found out that DBL_MIN and FLT_MIN does not provide the absolute minimum of the data type but rather the positive minimum value :) I'll just add a dash symbol manually and use DBL/FLT_MAX for the minimum value. Though its not ethical/code of stackexchange to say thank you in the comments, I'll still thank you, i've been stuck on this question for the past week :'(
    – UnfreeHeX
    Commented Nov 16, 2021 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.