Offline
Newbie
Karma: 0
Posts: 10
|
 |
« on: July 31, 2011, 06:58:46 pm » |
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. Thanks in advance. 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
Jr. Member
Karma: 0
Posts: 94
Arduino rocks
|
 |
« Reply #1 on: July 31, 2011, 07:28:54 pm » |
How about: #include <math.h>
int getDigit(long number, int place) { return ((number / long(pow(10, place - 1))) % 10); }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #2 on: July 31, 2011, 07:53:06 pm » |
So I used your code like such: #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
Jr. Member
Karma: 0
Posts: 94
Arduino rocks
|
 |
« Reply #3 on: July 31, 2011, 08:22:33 pm » |
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: 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
Tesla Member
Karma: 96
Posts: 6368
|
 |
« Reply #4 on: July 31, 2011, 09:07:57 pm » |
pow() returns a float. Arduino is MUCH happier with integers: 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
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #5 on: July 31, 2011, 10:30:35 pm » |
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: 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: 9 8 7 6 5 0 0 0 0 0
8 4 6 3 8 4 7 4 1 2
|
|
|
|
|
Logged
|
|
|
|
|
Australia
Offline
Jr. Member
Karma: 0
Posts: 94
Arduino rocks
|
 |
« Reply #6 on: July 31, 2011, 10:43:43 pm » |
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
Full Member
Karma: 0
Posts: 222
|
 |
« Reply #7 on: August 01, 2011, 03:23:01 am » |
This feels suspiciously like a homework assignment, but I'll bite: 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
Full Member
Karma: 0
Posts: 222
|
 |
« Reply #8 on: August 01, 2011, 03:26:45 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).
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #9 on: August 01, 2011, 03:43:46 am » |
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 10
|
 |
« Reply #10 on: August 01, 2011, 06:30:49 pm » |
Thanks all of you, I really appreciated!! and no its not for school. lol, you all guys really know this stuff. Kudos!! 
|
|
|
|
|
Logged
|
|
|
|
|
|