While uploading "An error occured" and serial monitor got stuck...or?

Status
Not open for further replies.

mcc

Well-known member
Hi,

I am playing around with my Teensy 3.6, which I received today!

Here is the code in question:
Code:
#include <math.h>
 
// set this to the hardware serial port you wish to use
#define HWSERIAL Serial1

double num=3;
double divi=2;


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

// the loop routine runs over and over again forever:
void loop() {

  double lim=sqrt(num);
  int flg=0;
  divi=2;
  while( divi < lim ) {
    if( 0.0 != fmod( num , divi )) {
      divi += 1.0;
      continue;
    }
    flg=1;
    break;
  }
  if( ! (flg != 0.0) ) {
    Serial.print( num );
    Serial.print( "," );
  }
  num += 1.0;
}

I wanted to see the FPU running fast...so I wrote this program which
calculates prime numbers and print them if one is found. It is a very simple program -
nothing compared to "The great Mersenne prime search" or such ;)

The program runs ... and stops at 7949...at least this is the last number which is
visible with the serial monitor.
I replaced the "," with a "\n"...but the same happens...the output or the program got
stuck around ~7800...~7950.

But I cant find the reason for this...
How can I prevent the stop of the loop?

Another thing:
When uploading the code, the Arduino IDE prints:
"An error occurred while uploading the sketch"
then the uploader pops up briefly and the sketch will be flashed...

How can I fix that?

Environment:
GENTOO Linux (updated on a daily basis)
UDEV rules are installed
Arduino IDE 1.8.9
Standard USB (not 3.0).

Thanks a lot for any help in advance!
Cheers!
Meino
 
I'm running it here. Indeed sometimes it gets stuck. Not sure why, but it's looking like a problem on the PC side with the incoming data before the serial monitor reopens. Can't do much more on that right now, but I'm putting it on my list of issues to investigate.

In the meantime, here's a slightly modified copy. Try running this.

Code:
#include <math.h>

double num=3;
double divi=2;

void setup() {
    Serial.begin(115200);
    if (!Serial) ; // wait for serial monitor
    delay(1000);
}

// the loop routine runs over and over again forever:
void loop() {

  double lim=sqrt(num);
  int flg=0;
  divi=2;
  while( divi < lim ) {
    if( 0.0 != fmod( num , divi )) {
      divi += 1.0;
      continue;
    }
    flg=1;
    break;
  }
  if( ! (flg != 0.0) ) {
    Serial.print( num );
    Serial.println( "," );
  }
  num += 1.0;
}

Here's what I'm seeing after it has run for a couple minutes (and still going).

sc.png
 
Good evening, Paul, :)

Thanks a lot for the modified code. Tried it here, and TADA! It runs and runs and runs... :)
Nice! I am very happy with my Teensy 3.6.
A real FPU makes things so much better... <grin>

Have a nice weekend!
Cheers!
Meino
 
This program probably isn't using the FPU, which only accelerates 32 bit float on Teensy 3.6. All 64 bit double math is still done with software.

Teensy 4.0 will have a FPU that does both. But even there, 32 bit float runs ~2X faster.
 
Hi Paul,

than I am twice as happy: A board, which integer arithmetic is so fast that it can be confused with a FPU, is GREAT!
(I understood the technical implications you mentioned... )

Ok, then...switching the doubles to floats will not do the trick, I think:
math.h includes:
double fabs (double __x) // absolute value of a float
double fmod (double __x, double __y) // floating point modulo
double modf (double __value, double *__iptr) // breaks the argument value into
// integral and fractional parts
double sin (double __x) // returns sine of x
double sqrt (double __x) // returns square root of x
double tan (double __x) // returns tangent of x
double exp (double __x) // function returns the exponential value of x.
double atan (double __x) // arc tangent of x
double atan2 (double __y, double __x) // arc tangent of y/x
double log (double __x) // natural logarithm of x
double log10 (double __x) // logarithm of x to base 10.
double pow (double __x, double __y) // x to power of y
double square (double __x) // square of x


Everything is doublyfied...the compiler will cast it implicetly to double, circumvent the FPU this way, return the result and cast it back. I think, this will happen.
Or in other words...the FPU will only help on primary school math ... that is +,-,/,* .
"Higher math" will use doubles (as shown above) and ist not handled by the FPU therefore .
Or is there a Teensy-Mathlib, which does all that based on floats instead of doubles?

In which of my feet I have just shoot myself? ;)

Cheers!
Meino

PS: Just found this thread:
https://forum.pjrc.com/threads/40551-How-to-be-sure-I-m-using-the-FPU-in-T3-5-3-6
...but it is focussed on sqrt() and friends mainly...


PPS:
I changed the code and hopefully the FPU is now involved.
Will see, whether I get it even faster... :)

Code:
#include <arm_math.h>

float num=3;
float divi=2;

void setup() {
    Serial.begin(115200);
    if (!Serial) ; // wait for serial monitor
    delay(1000);
}

// the loop routine runs over and over again forever:
void loop() {

  float lim=sqrtf(num);
  int flg=0;
  divi=2;
  while( divi < lim ) {
    if( 0.0 != fmod( num , divi )) {
      divi += 1.0;
      continue;
    }
    flg=1;
    break;
  }
  if( ! (flg != 0.0) ) {
    Serial.print( num );
    Serial.println( "," );
  }
  num += 1.0;
}
 
Last edited:
Arduino IDE Serial Monitor again ? Because, with custom makefile outside of Arduino IDE your code runs.. as long enough to have me willing to seek a Netflix serie again ? ;-)

Last output

Code:
1155863.00,1155899.00,1155901.00,1155907.00,1155919.00,1155923.00,1155929.00,1155937.00,1155943.00,1155953.00,1155961.00,1155967.00,1155971.00,1155977.00,1155997.00,1156009.00,1156013.00,1156019.00,1156031.00,1156033.00,1156037.00,1156039.00,1156073.00,1156079.00,1156087.00,1156097.00,1156109.00,1156121.00,1156151.00,1156157.00,1156171.00,1156217.00,1156229.00,1156231.00,1156249.00,1156261.00,1156271.00,1156291.00,1156297.00,1156303.00,1156307.00,1156327.00,1156333.00,1156343.00,1156367.00,1156369.00,1156387.00,1156403.00,1156423.00,1156427.00,1156429.00,1156451.00,1156453.00,1156457.00,1156483.00,1156501.00,1156523.00,1156537.00,1156541.00,1156553.00,1156567.00,1156591.00,1156613.00,1156627.00,1156633.00,1156637.00,1156643.00,1156681.00,1156699.00,1156709.00,1156711.00,1156721.00,1156741.00,1156747.00,1156751.00,1156769.00,1156783.00,1156801.00,1156807.00,1156819.00,1156823.00,1156847.00,1156849.00,1156873.00,1156907.00,1156927.00,1156949.00,1156963.00,1156997.00,1157011.00,1157017.00,1157033.00,1157053.00,1157059.00,1157063.00,1157069.00,1157077.00,1157099.00,1157111.00,1157131.00,1157159.00,1157171.00,1157179.00,1157183.00,1157201.00,1157203.00,1157209.00,1157213.00,1157227.00,1157237.00,1157243.00,1157251.00,1157257.00,1157263.00,1157279.00,1157293.00,1157327.00,1157333.00,1157339.00,1157341.00,1157357.00,1157363.00,1157369.00,1157381.00,1157393.00,1157413.00,1157437.00,1157449.00,1157489.00,1157491.00,1157503.00,1157531.00,1157539.00,1157557.00,1157579.00,1157591.00,1157609.00,1157621.00,1157627.00,1157641.00,1157669.00,1157671.00,1157699.00,1157701.00,1157711.00,1157713.00,1157729.00,1157747.00,1157749.00,1157759.00,1157771.00,1157773.00,1157791.00,1157831.00,1157833.00,1157837.00,1157839.00,1157851.00,1157869.00,1157873.00,1157899.00,1157929.00,1157953.00,1157969.00,1157977.00,1157987.00,1158007.00,1158011.00,1158037.00,1158071.00,1158077.00,1158089.00,1158121.00,1158133.00,1158139.00,1158161.00,1158187.00,1158197.00,1158203.00,1158217.00,1158247.00,1158251.00,1158263.00,1158271.00,1158293.00,1158301.00,1158307.00,1158317.00,1158323.00,1158341.00,1158361.00,1158383.00,1158389.00,1158401.00,1158407.00,1158419.00,1158427.00,1158457.00,1158461.00,1158467.00,1158473.00,1158481.00,1158491.00,1158523.00,1158529.00,1158539.00,1158541.00,1158551.00,1158569.00,1158587.00,1158593.00,1158607.00,1158611.00,1158613.00,1158617.00,1158629.00,1158643.00,1158653.00,1158673.00,1158679.00,1158683.00,1158713.00,1158719.00,1158743.00,1158757.00,1158761.00,1158769.00,1158799.00,1158821.00,1158823.00,1158827.00,1158841.00,1158847.00,1158863.00,1158881.00,1158887.00,1158923.00,1158953.00,1158961.00,1158977.00,1158991.00,1159001.00,1159007.00,1159027.00,1159031.00,1159049.00,1159063.00,1159073.00,1159079.00,1159087.00,1159091.00,1159127.00,1159139.00,1159153.00,1159187.00,1159189.00,1159199.00,1159201.00,1159229.00,1159231.00,1159241.00,1159243.00,1159259.00,1159271.00,1159283.00,1159303.00,1159337.00,1159339.00,1159381.00,1159393.00,1159397.00,1159421.00,1159423.00,1159429.00,1159447.00,1159463.00,1159489.00,1159517.00,1159523.00,1159531.00,1159541.00,1159577.00,1159583.00,1159597.00,1159601.00,1159633.00,1159649.00,1159661.00,1159663.00,1159709.00,1159721.00,1159777.00,1159787.00,1159789.00,1159811.00,1159813.00,1159843.00,1159853.00,1159861.00,1159877.00,1159889.00,1159901.00,1159909.00,1159919.00,1159967.00,1159973.00,1159981.00,1159993.00,1159997.00,1160009.00,1160039.00,1160041.00,1160057.00,1160077.00,1160111.00,1160129.00,1160141.00,1160147.00,1160161.00,1160167.00,1160179.00,1160207.00,1160213.00,1160219.00,1160221.00,1160227.00,1160251.00,1160279.00,1160287.00,1160297.00,1160303.00,1160309.00,1160317.00,1160351.00,1160359.00,1160363.00,1160371.00,1160407.00,1160413.00,1160429.00,1160443.00,1160447.00,1160449.00,1160459.00,1160473.00,1160479.00,1160491.00,1160503.00,1160513.00,1160519.00,1160543.00,1160567.00,1160569.00,1160581.00,1160597.00,1160611.00,1160639.00,1160659.00,1160681.00,1160689.00,1160713.00,1160717.00,1160749.00,1160771.00,1160807.00,1160813.00,1160837.00,1160839.00,1160867.00,1160893.00,1160903.00,1160911.00,1160927.00,1160941.00,1160953.00,

**EDIT**

Had a smoke on the backyard, then

Code:
2276411.00

**EDIT 2**

Code:
001597.00,3001613.00,3001631.00,3001633.00,3001643.00,3001657.00,3001681.00,3001697.00,3001711.00,3001721.00,3001727.00,3001759.00,3001769.00,3001777.00,3001811.00,3001837.00,3001853.00,3001879.00,3001883.00,3001907.00,3001909.00,3001919.00,3001927.00,3001931.00,3001939.00,3001963.00,3001991.00,3002003.00,3002039.00,3002089.00,3002101.00,3002107.00,3002113.00,3002117.00,3002123.00,3002147.00,3002161.00,3002173.00,3002177.00,3002189.00,3002191.00,3002221.00

3XXXXXX reached Ok, got next episode, Teensy off 'til tomorrow :)
 
Last edited:
OLD POST UPDATE: Looking to use this simple code to test something else for ability to restart ...

Using this web page the results can be confirmed: numberempire.com/3590089

For that value 3590089 - it confirms the answer, the prior and following prime and the counted number of primes to that point:
Code:
Is prime?	YES (300000th prime)
Previous prime	4256227
Next prime	4256249

With this output:
Code:
4256227.00,	299999,
[B]4256233.00,	300000,[/B]
4256249.00,	300001,

There were ERRORS before with some earlier code - not showing 2 and it WAS including 4 - and the first ref to that linked page showed the count was WAY off IIRC - so other non-Primes were slipping through.

Here is the quick edits to resolve those issues and see all is well up to 256067 primes:
Code:
#include <arm_math.h>

float num = 3;
float divi = 2;
static int32_t pCnt = 1;

void setup() {
	Serial.begin(115200);
	while (!Serial && millis() < 4000 );
	Serial.println("\n" __FILE__ " " __DATE__ " " __TIME__);
		Serial.print( 2 );
		Serial.print( ",\t" );
		Serial.print( pCnt );
		Serial.println( "," );
}

// the loop routine runs over and over again forever:
void loop() {

	float lim = sqrtf(num);
	int flg = 0;
	divi = 3;
	while ( divi <= lim ) {
		if ( 0.0 != fmod( num , divi )) {
			divi += 2.0;
			continue;
		}
		flg = 1;
		break;
	}
	if ( ! (flg != 0.0) ) {
		pCnt++;
		Serial.print( num );
		Serial.print( ",\t" );
		Serial.print( pCnt );
		Serial.println( "," );
	}
	num += 2.0;
}

also:
Code:
9999991.00,	664579,
[B]10000019.00,	664580,[/B]
10000079.00,	664581,
shows:
Code:
Is prime?	YES (664580th prime)
Previous prime	9999991
Next prime	10000079
 
Status
Not open for further replies.
Back
Top