Go Down

### Topic: Programming tomb!!! (Read 1 time)previous topic - next topic

#### ejahable

##### Aug 01, 2011, 01:58 amLast Edit: Aug 01, 2011, 02:01 am by ejahable Reason: 1
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.

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.

Code: [Select]
`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));}}`

#### crimony

#1
##### Aug 01, 2011, 02:28 am

Code: [Select]
`#include <math.h>int getDigit(long number, int place){    return ((number / long(pow(10, place - 1))) % 10);}`

#### ejahable

#2
##### Aug 01, 2011, 02:53 am
So I used your code like such:

Code: [Select]
`#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

#### crimony

#3
##### Aug 01, 2011, 03:22 am
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: [Select]
`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);}`

#### johnwasser

#4
##### Aug 01, 2011, 04:07 am
pow() returns a float.  Arduino is MUCH happier with integers:

Code: [Select]
`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]
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp

#### nickgammon

#5
##### Aug 01, 2011, 05:30 am

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.

6 ft surely?

Try converting to a string:

Code: [Select]
`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 setupvoid loop (){}`

Example output:

Code: [Select]
`98765000008463847412`
Please post technical questions on the forum, not by personal message. Thanks!

#### crimony

#6
##### Aug 01, 2011, 05:43 am
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).

#### Utopia

#7
##### Aug 01, 2011, 10:23 am
This feels suspiciously like a homework assignment, but I'll bite:

Code: [Select]
`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...

#### Utopia

#8
##### Aug 01, 2011, 10:26 am
... 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).

#### nickgammon

#9
##### Aug 01, 2011, 10:43 am

Code: [Select]
`... // sanity clause...`

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

http://en.wikipedia.org/wiki/There_Ain't_No_Sanity_Clause
Please post technical questions on the forum, not by personal message. Thanks!