Pages: [1]   Go Down
Author Topic: Programming tomb!!!  (Read 968 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I will be 4ft underground if I don't get this to work. I spent 5 days already trying to figure out solutions but is just not working.  smiley-sad

I need an object that receives two parameters:
   1) a number (0 to 99999 "long type")
   2) a place number (0 to 5 "int type")  
with these parameters it returns the digit of the number determine by the place number.

example 1:
 1)  number:           56789
 2)  place number:  1
  it returns 9    

example 2:
 1)  number:           56789
 2)  place number:  2
  it returns 8

I believe that I made my point, I added my code below. Nevertheless I can't get it to work without overflowing. Any suggestions will be appreciated.

Thanks in advance.

Code:
int getDigit(long number,int place){
  if(place == 1){return (number-((int(number*0.1))*10));}
  else if(place == 2){return (int((number-((int(number*0.01))*100))*0.1));}
  else if(place == 3){return (int((number-((int(number*0.001))*1000))*0.01));}
  else if(place == 4){return (int((number-(long(int(number*0.0001)))*10000)*0.001));}
  else{return (int(number*0.0001));}
}
« Last Edit: July 31, 2011, 07:01:59 pm by ejahable » Logged

Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 100
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

How about:

Code:
#include <math.h>

int getDigit(long number, int place)
{
    return ((number / long(pow(10, place - 1))) % 10);
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So I used your code like such:

Code:
#include <math.h>
boolean boo = true;
void setup(){
  Serial.begin(9600);
}
void loop(){
  while(boo){
    delay(4000); 
    Serial.println(getDigit(56789L,1));
    Serial.println(getDigit(56789L,2));
    Serial.println(getDigit(56789L,3));
    Serial.println(getDigit(56789L,4));
    Serial.println(getDigit(56789L,5));
    boo = false;
    }
}

int getDigit(long number, int place)
{
    return ((number / long(pow(10, place - 1))) % 10);
}

and got as result:
9
8
3
6
5

Your code is ingenious but it stills has problems. Thanks
Logged

Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 100
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't have my Arduino here so I can't test, but it works fine on Linux using int as long and short as int. Must be an Arduino integer or math library issue.

You will need to split the functionality up and see at what point it is failing:

Code:
int getDigit(long number, int place)
{
    double p = pow(10, place - 1);
    long l = long(p);
    long n = number / l;
    long m = n % 10;

    // Debug output
    Serial.print("getDigit() called, number = ");
    Serial.print(number);
    Serial.print(", place = ");
    Serial.print(place);
    Serial.print(", l = ");
    Serial.print(l);
    Serial.print(", n = ");
    Serial.print(n);
    Serial.print(", m = ");
    Serial.println(m);

    return int(m);
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

pow() returns a float.  Arduino is MUCH happier with integers:

Code:

int getDigit(long number, int place)
{
   for (; place>1; place--)
      number /= 10;   // Shift right one decimal digit
 
   return number % 10;  // Return rightmost decimal digit
}
[/quote]
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19358
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I will be 4ft underground if I don't get this to work. I spent 5 days already trying to figure out solutions but is just not working.  smiley-sad

6 ft surely?

Try converting to a string:

Code:
int getDigit (long number, int place)
 {
 char buf [12];
 ltoa (number, buf, 10);
 // sanity clause
 if (place <= 0 || place > strlen (buf))
   return 0;
 // extract digit
 return (buf [strlen (buf) - place] - '0');
 }  // end of getDigit
 
void setup ()
{
  Serial.begin (115200);
 
  for (int i = 1; i <= 10; i++)
    Serial.println (getDigit (56789L, i));

  Serial.println ();
 
  for (int i = 1; i <= 10; i++)
    Serial.println (getDigit (2147483648L, i));
 
}  // end of setup

void loop ()
{}

Example output:

Code:
9
8
7
6
5
0
0
0
0
0

8
4
6
3
8
4
7
4
1
2
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 100
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
pow() returns a float.  Arduino is MUCH happier with integers

Agreed, but I set myself the goal of trying to do it in one line (not counting the #include).
Logged

Victoria, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 222
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This feels suspiciously like a homework assignment, but I'll bite:

Code:
unsigned int homework(unsigned long x,unsigned int m)
{
  while (--m)
    x/=10;
  return x%10;
}

Will work for 1-max digits in number, unsigned only; no guarantees outside of that...
Logged

Victoria, BC, Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 222
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

... just noticed johnwasser already wrote code that works, so didn't need to include mine - however, it's already done, it's another variation, so I'll leave it be (sorry john - should have checked more carefully).
Logged

Global Moderator
Melbourne, Australia
Online Online
Brattain Member
*****
Karma: 511
Posts: 19358
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
...
 // sanity clause
...

http://www.marx-brothers.org/info/quotes.htm

http://en.wikipedia.org/wiki/There_Ain't_No_Sanity_Clause
Logged

http://www.gammon.com.au/electronics

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks all of you, I really appreciated!! and no its not for school.  smiley-yell 
lol, you all guys really know this stuff. Kudos!!  smiley-cool
Logged

Pages: [1]   Go Up
Jump to: