Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: float to string issue

  1. #1
    Junior Member
    Join Date
    Dec 2016
    Posts
    19

    float to string issue

    Just curious if anyone has come across this issue. I can't seem to convert a float to a string. My test code is below. I am running with PlatformIO with the latest updates. If I run the same code in Arduino IDE, it works fine. Trying to understand why it is failing in PlatformIO.

    Hardware = Teensy3.6

    Code:
    #include <arduino.h>
    
    int main()
    {
    	// Rates: 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, or 115200
    	Serial.begin(9600);
    
    	int value1 = -12345;
    	int value2 = 9876;
    	float pi = 3.141596;
    	char buffer[32];
    	int copied = 0;
    
    	while (true)
    	{
    		memset(buffer, 0x00, sizeof(buffer));
    		copied = snprintf(buffer, sizeof(buffer), "%d", value1);
    		Serial.print("copied: ");
    		Serial.println(copied);
    		Serial.print("buffer: ");
    		Serial.println(buffer);
    
    		memset(buffer, 0x00, sizeof(buffer));
    		copied = snprintf(buffer, sizeof(buffer), "%d", value2);
    		Serial.print("copied: ");
    		Serial.println(copied);
    		Serial.print("buffer: ");
    		Serial.println(buffer);
    
    		memset(buffer, 0x00, sizeof(buffer));
    		copied = snprintf(buffer, sizeof(buffer), "%.4f", pi);
    		Serial.print("copied: ");
    		Serial.println(copied);
    		Serial.print("buffer: ");
    		Serial.println(buffer);
    
    		delay(5000);
    	}
    
    	return 0;
    }
    Output:
    copied: 6
    buffer: -12345
    copied: 4
    buffer: 9876
    copied: 0
    buffer:

    I tried using a template ostringstream method as well. It it crashes on ss << Number when the number is a float.

    Code:
    template <typename T> std::string to_string(const T& Number)
    {
    	std::ostringstream ss;
    	ss << Number;
    	return ss.str();
    }
    Last edited by Joe31093; 01-27-2017 at 07:39 PM.

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    5,422
    You might want to ask on the Platform IO forums.

    But if I were to take a guess, the default print library may not support floating point. I remember with some compilers there was an option enable or disable floating point. Maybe there is an option to enable it?

  3. #3
    Junior Member
    Join Date
    Dec 2016
    Posts
    19
    The first two values are %d and the last one is %f. Converting pi is where it fails. Sstream fails as well.

  4. #4
    Senior Member
    Join Date
    May 2015
    Location
    San Francisco
    Posts
    157
    I needed to add this to my sketch to get printf to support floats on teensy:

    asm(".global _printf_float");

    Works in platformio for me.

  5. #5
    Junior Member
    Join Date
    Dec 2016
    Posts
    19
    Thanks blackketter. That seems to have resolved the issue with snprintf.

    Anyone have any idea on why the stringstream << float is failing?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •