sipeed / Maixduino

Arduino port on Maix board ( k210 )
https://maixduino.sipeed.com
Other
214 stars 93 forks source link

Weird bug with String() function #40

Open phjo33 opened 5 years ago

phjo33 commented 5 years ago

I wrote a very simple benchmark to compare several microcontrollers, including kendryte k210. Results are sent to Serial. It took me a long time to figure out why I had no output, as I thought the culprit was the Serial to usb implementation (or a driver problem with my computer)

I'll attach the code. This code does not work : no blinking led (which I added to see if the code got executed or not), not even once, and no output to the console.

By replacing String(y) with just y on line 98, it does work.

This baffles me, especially as the String function gets called a few lines earlier, with a float then a double and a precision of 16 digits, with no problem whatsoever...

The same code, with String(y), ran perfectly with atmega 2560, stm32f103, stm32f446re, esp32...

Trying to reduce the code to just keep the offending section, then the String() function works correctly...

It seems we can't join .cpp file, so here is my main.cpp pasted :

`#include

int isPrime(int n) { for (int i = 2; i < n; i++) { if (n % i == 0) return 0; } return 1; }

int countPrimesUpTo(int n) { int nb = 0; for (int i = 2; i < n; i++) // no reason to aim at efficiency here... { if (isPrime(i)) { nb++; } } return nb; }

float precisionFloat(int steps) { float x = 2; for (int i=0; i < steps; i++) { x = sqrtf(x); } for (int i = 0; i < steps; i++) { x *= x; } return x; }

double precisionDouble(int steps) { double x = 2; for (int i=0; i < steps; i++) { x = sqrt(x); } for (int i = 0; i < steps; i++) { x *= x; } return x; }

float calcFloat(int n) { float x; for (int i=0; i < n; i++) { x = 0.1; for (int j = 0; j < 1000; j++) { x = (float)4 x ((float)1 - x); } } return x; }

double calcDouble(int n) { double x; for (int i=0; i < n; i++) { x = 0.1; for (int j = 0; j < 1000; j++) { x = (double)4 x ((double)1 - x); } } return x; }

void setup() { // put your setup code here, to run once: Serial.begin(57600); pinMode(LED_BUILTIN, OUTPUT); }

void loop() { // put your main code here, to run repeatedly: Serial.println("beginning tests"); digitalWrite(LED_BUILTIN, LOW); unsigned long StartTime = millis(); int nb = countPrimesUpTo(20000); unsigned long elapsedTime = millis() - StartTime; Serial.print(nb); Serial.println(" primes until 20000, time spent "); Serial.print(elapsedTime); Serial.println(" milliseconds"); Serial.println(String(precisionFloat(15),8)); Serial.println(String(precisionDouble(25), 16)); StartTime = millis(); float x = calcFloat(1000); elapsedTime = millis() - StartTime; Serial.print(elapsedTime); Serial.println(" milliseconds for 1000 iters (float)"); Serial.println(String(x, 8)); digitalWrite(LED_BUILTIN, HIGH); StartTime = millis(); double y = calcDouble(1000); elapsedTime = millis() - StartTime; Serial.print(elapsedTime); Serial.println(" milliseconds for 1000 iters (double)"); Serial.println(String(y)); digitalWrite(LED_BUILTIN, HIGH); delay(2000); }`

A precision : I'm using platformio with vscode.