Check if there´s an number after the comma

Hi!

How can I make the Arduino check if there´s an number after the comma?

(if it´s 3.25434, not just 3)

So, if it´s a natural number without digits after the comma...

Someone knows how to?

it is a data type question

Integers... if you define the number as an int or a long, there are never numbers after the decimal point.

Floating point numbers... a float will have numbers after the decimal

The number ic changing continously, saved in a float;
But a float can also just be a 3, so how can I check if there´s a number that´s not a 0 after the . ?

check to see if it larger than three

if (myFloat > 3)
{
//do something
}

like that....

f = 3.14;

if (f == int(f) )
{ 
  Serial.print("// no digits after , ");
}
else
...
1 Like

Thanks!

But I dont know what I´m doing wrong…
I´m trying to make the arduino calculate every prime number (that can only be divided by itself and 1)
But it won´t work…

int var = 9;
int A;
float B;
float Max;

void setup() {

  Serial.begin(9600);
}


void loop() {

A = 0; //Teiler
B = 0;
var++;
Max = sqrt(var);



while (A <= Max) {
  
  while(A == 0) {
    A++;
  }

  B = var / A;
  if (B == int(B) ) {
    Serial.println("Prime Number:");
   Serial.println(var); 

  }

 A++; 
} 


  

}

Ahhhh. that is integer math, right!

are you familiar with the modulo (%) operator? it tells you the value of the remainder of division:

5 % 2 = 1

10 % 5 = 0

Unfortunately not...
How can I include that?

take a look at this and try running it:

void setup() 
{
  Serial.begin(9600);
}


void loop() 
{
  prime();
  while(1){
  };
}
void prime() 
{
  for (int i=2; i<100; i++) 
  {
    bool prime=true;
    for (int j=2; j*j<=i; j++)
    {
      if (i % j == 0) 
      {
        prime=false;
        break;    
      }
    }   
    if(prime) 
    {
      Serial.print(i);
      Serial.println(" is a prime");
    }
  }
}

THANKS!
I can´t really understand that code, but i will :%

Many thanks :smiley:

modulo is the remainder of the division of two integers:

5/5 = 1 with a remainder of 0 so 5%5 = 0
5/3 = 1 with a remainder of 2 so 5%3 = 2
5/2 = 2 with a remainder of 1 so 5%2 = 1

10/10 = 1 with a remainder of 0 so 10%10 = 0
10/3 = 3 with a remainder of 1 so 10%3 = 1

Hm...
How can I make it count up infinitely, the library BigNumber doesn´t supports %...

You know how?

Mindstormsnxt:
Hm...
How can I make it count up infinitely, the library BigNumber doesn´t supports %...

You know how?

wow, that's a big leap forward. Even BigNumber cannot count that high :blush:

what are you trying to do exactly? first you were checking to see if a number was a prime, I believe.

I want to hang a display to it and see it counting up... :smiley:

use an unsigned long and you can go up to 4,294,967,295.

the largest 4 byte prime is 4,294,967,291, FYI (close right?)

put a little delay in to have it take a long long time to get there and start over:

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


void loop() 
{
  prime();
}
void prime() 
{
  for (unsigned long i=2; i<4294967295; i++) 
  {
    bool prime=true;
    for (unsigned long j=2; j*j<=i; j++)
    {
      if (i % j == 0) 
      {
        prime=false;
        break;    
      }
    }   
    if(prime) 
    {
      Serial.print(i);
      Serial.println(" is a prime");
      delay(1000);
    }
  }
}
  while(A == 0) {
    A++;
  }

How many times is this going to iterate? After setting A to 0, why even bother testing for A == 0?

Mindstormsnxt:
How can I make the Arduino check if there´s an number after the comma?

(if it´s 3.25434, not just 3)

So, if it´s a natural number without digits after the comma…

That is a decimal place / period / full stop. Not a comma.


How can I make it count up infinitely, the library BigNumber doesn´t supports %…

Actually, it does. We went through this a while back:

#include <BigNumber.h>

const char FIRST_PRIME_TO_USE [] =  "4294967291";

BigNumber candidate;
BigNumber one (1);
BigNumber two (2);

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

  while (!Serial) { 
  }

  BigNumber::begin ();  

  Serial.println ();
  Serial.println ();
  Serial.println ("Starting.");

  candidate = BigNumber (FIRST_PRIME_TO_USE);
  if (candidate % two == 0)
    candidate++;
}

void loop() 
{

  for ( ; candidate < BigNumber ("9999999999999999"); candidate += 2)
    showIfPrime (candidate);

  candidate = 3;  // back to start!
}  // end of loop

void rng (BigNumber & result)
{
  result = BigNumber (random ());  
}

bool Miller(BigNumber source, int certainty)
{

  // these tests not needed if we start > 3 and use odd numbers only
  /*
  if(source == 2 || source == 3)
    return true;
  if(source < two || source % two == 0)
    return false;
  */
  
  BigNumber d = source - one;
  int s = 0;

  while(d % two == 0)
  {
    d /= two;
    s += one;
  }

  BigNumber a;

  for(int i = 0; i < certainty; i++)
  {
    do
    {
      rng (a);
    } 
    while(a < two || a >= source - two);

    BigNumber x = a.powMod (d, source);
    if(x == one || x == source - one)
      continue;

    for(int r = 1; r < s; r++)
    {
      x = x.powMod(two, source);
      if(x == one)
        return false;
      if(x == source - one)
        break;
    }

    if(x != source - one)
      return false;
  }

  return true;
}  // end of Miller

const byte DIGITS = 16;

unsigned long start;
unsigned long lastShown;
long found = 1; // Number we found

void showIfPrime (BigNumber num) 
{

  if (!Miller (num, 10))
    return;


  // make printable
  char * s = num.toString ();
  int len = strlen (s);
  
  char buf [max(DIGITS, len) + 1];
  if (len < DIGITS)
    {
    memset (buf, ' ', DIGITS - len);
    strcpy (&buf [DIGITS - len], s);  
    }
  else
    strcpy (buf, s);
  buf [max(DIGITS, len)] = 0;
  free (s);
  
  Serial.print(buf);
  Serial.print(" is prime. Took ");
  Serial.print(millis () - start);
  Serial.print (" mS. Found ");
  Serial.print (found);
  Serial.println (" primes.");
  start = millis ();

  found++;

  lastShown = millis ();

}  // end of showIfPrime

Output:

Starting.
      4294967291 is prime. Took 3876 mS. Found 1 primes.
      4294967311 is prime. Took 4961 mS. Found 2 primes.
      4294967357 is prime. Took 7990 mS. Found 3 primes.
      4294967371 is prime. Took 3896 mS. Found 4 primes.
      4294967377 is prime. Took 2917 mS. Found 5 primes.
      4294967387 is prime. Took 3457 mS. Found 6 primes.
      4294967389 is prime. Took 2433 mS. Found 7 primes.
      4294967459 is prime. Took 11152 mS. Found 8 primes.
      4294967477 is prime. Took 4512 mS. Found 9 primes.
      4294967497 is prime. Took 4773 mS. Found 10 primes.
      4294967513 is prime. Took 4275 mS. Found 11 primes.
      4294967539 is prime. Took 5700 mS. Found 12 primes.
      4294967543 is prime. Took 2833 mS. Found 13 primes.
      4294967549 is prime. Took 3053 mS. Found 14 primes.
      4294967561 is prime. Took 3669 mS. Found 15 primes.
      4294967563 is prime. Took 2407 mS. Found 16 primes.
      4294967569 is prime. Took 2948 mS. Found 17 primes.
      4294967597 is prime. Took 5759 mS. Found 18 primes.
      4294967627 is prime. Took 6058 mS. Found 19 primes.
      4294967639 is prime. Took 3806 mS. Found 20 primes.
      4294967653 is prime. Took 4045 mS. Found 21 primes.
      4294967681 is prime. Took 6161 mS. Found 22 primes.
      4294967687 is prime. Took 2923 mS. Found 23 primes.
      4294967701 is prime. Took 4034 mS. Found 24 primes.
      4294967723 is prime. Took 5149 mS. Found 25 primes.
      4294967759 is prime. Took 7070 mS. Found 26 primes.
      4294967779 is prime. Took 4948 mS. Found 27 primes.
      4294967783 is prime. Took 2835 mS. Found 28 primes.
      4294967791 is prime. Took 3445 mS. Found 29 primes.
      4294967797 is prime. Took 3130 mS. Found 30 primes.
      4294967821 is prime. Took 5337 mS. Found 31 primes.
      4294967857 is prime. Took 6787 mS. Found 32 primes.
      4294967861 is prime. Took 2692 mS. Found 33 primes.
      4294967867 is prime. Took 3009 mS. Found 34 primes.
      4294967891 is prime. Took 5307 mS. Found 35 primes.
      4294967969 is prime. Took 12540 mS. Found 36 primes.
      4294967977 is prime. Took 3232 mS. Found 37 primes.
      4294967983 is prime. Took 3081 mS. Found 38 primes.
      4294968001 is prime. Took 4833 mS. Found 39 primes.
      4294968017 is prime. Took 4377 mS. Found 40 primes.
      4294968019 is prime. Took 2503 mS. Found 41 primes.
      4294968023 is prime. Took 2829 mS. Found 42 primes.
      4294968059 is prime. Took 7230 mS. Found 43 primes.
      4294968101 is prime. Took 7682 mS. Found 44 primes.
      4294968127 is prime. Took 5823 mS. Found 45 primes.
      4294968143 is prime. Took 4418 mS. Found 46 primes.
      4294968149 is prime. Took 3088 mS. Found 47 primes.
      4294968173 is prime. Took 5470 mS. Found 48 primes.
      4294968187 is prime. Took 4286 mS. Found 49 primes.
      4294968199 is prime. Took 3878 mS. Found 50 primes.
      4294968211 is prime. Took 3842 mS. Found 51 primes.
      4294968233 is prime. Took 5214 mS. Found 52 primes.
      4294968239 is prime. Took 3164 mS. Found 53 primes.
      4294968257 is prime. Took 4898 mS. Found 54 primes.
      4294968283 is prime. Took 5881 mS. Found 55 primes.
      4294968287 is prime. Took 2959 mS. Found 56 primes.
      4294968289 is prime. Took 2652 mS. Found 57 primes.
      4294968317 is prime. Took 6263 mS. Found 58 primes.
      4294968329 is prime. Took 3672 mS. Found 59 primes.
      4294968331 is prime. Took 2399 mS. Found 60 primes.
      4294968347 is prime. Took 4253 mS. Found 61 primes.
      4294968371 is prime. Took 5293 mS. Found 62 primes.
      4294968389 is prime. Took 4435 mS. Found 63 primes.
      4294968401 is prime. Took 3712 mS. Found 64 primes.
      4294968409 is prime. Took 3261 mS. Found 65 primes.
      4294968413 is prime. Took 2716 mS. Found 66 primes.
      4294968479 is prime. Took 10937 mS. Found 67 primes.
      4294968481 is prime. Took 2529 mS. Found 68 primes.
      4294968521 is prime. Took 7537 mS. Found 69 primes.
      4294968533 is prime. Took 3841 mS. Found 70 primes.
      4294968583 is prime. Took 8917 mS. Found 71 primes.
      4294968607 is prime. Took 5422 mS. Found 72 primes.
      4294968637 is prime. Took 6228 mS. Found 73 primes.
      4294968683 is prime. Took 8428 mS. Found 74 primes.
      4294968697 is prime. Took 4276 mS. Found 75 primes.
      4294968719 is prime. Took 5234 mS. Found 76 primes.
      4294968739 is prime. Took 4922 mS. Found 77 primes.
      4294968767 is prime. Took 6259 mS. Found 78 primes.
      4294968803 is prime. Took 7246 mS. Found 79 primes.
      4294968809 is prime. Took 3188 mS. Found 80 primes.
      4294968827 is prime. Took 4904 mS. Found 81 primes.
      4294968833 is prime. Took 3360 mS. Found 82 primes.
      4294968857 is prime. Took 5318 mS. Found 83 primes.
      4294968869 is prime. Took 3770 mS. Found 84 primes.
      4294968877 is prime. Took 3304 mS. Found 85 primes.
      4294968883 is prime. Took 3038 mS. Found 86 primes.
      4294968929 is prime. Took 8480 mS. Found 87 primes.
      4294968949 is prime. Took 5003 mS. Found 88 primes.

[quote author=Nick Gammon link=topic=269536.msg1900402#msg1900402 date=1411942294]

Mindstormsnxt:
How can I make the Arduino check if there´s an number after the comma?

(if it´s 3.25434, not just 3)

So, if it´s a natural number without digits after the comma...

That is a decimal place / period / full stop. Not a comma.[/quote]In some parts of Europe, a comma is used as the decimal separator.

I understand that, but in the example given it appeared that truncation was wanted after the period.

In any case this is a classic X-Y problem:

http://xyproblem.info/

What was wanted (X):

I´m trying to make the arduino calculate every prime number …

What was asked (Y):

How can I make the Arduino check if there´s an number after the comma?

And even Y was wrong because it was a period and not a comma.