|
After a long trying, I found this snippet:
int number = 1024;
int digit1 = ((number / 1000) % 10);
int digit2 = ((number / 100) % 10);
int digit3 = ((number / 10) % 10);
int digit4 = ((number / 1) % 10);
|
|
|
|
|
ant-damage wrote: After a long trying, I found this snippet:
... which is great until you give it a number like 456789.
Or if you try it with 23, you'll end up with zeroes.
As others have said, a loop will work no matter what number you throw at it.
Days spent at sea are not deducted from one's alloted span - Phoenician proverb
|
|
|
|
|
You were right. I've just created a test program to see if that piece of code was doing its paper.
I made the test so that it writes to a file the number of failed comparisions.
The result file had 200MB of length, so I think it's better to create a new algorithm to do that
|
|
|
|
|
Please show me an example of what you saying.
I'm not american nor british.
|
|
|
|
|
ant-damage wrote: I'm not american nor british
I'll give you an example implementation in pseudo-code then:
arrayOfInts ToDigits(int value) {
bool negative=value<0;
if (negative) value=-value;
ListOfInts digits=new ListOfInts;
do {
int mod=value%10;
digits.Add(mod);
value/=10;
} while (value!=0);
if (negative) digits.Add(specialCodeForMinusSign);
return digits.Reverse.ToArray;
}
PS: I am aware it would fail when fed int.MinValue
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
What the purpose of this algorithm?
Do you ever saw those 7-segment led displays in your tv, stereo hi-fi, vcr or dvd?
I have one of those but with 4 digits in one display, and those digits are in multiplexed mode, so I can only iterate with one at a time.
I won't use negative numbers, because I will use that display to make a digital counter/timer.
By the way, thanks for the example. I'm now ready to create an optimized one.
|
|
|
|
|
Oh great. This thread holds 20 messages already, and now you come with some details. I'm out of here.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Please don't remove messages that have replies attached; it is against the forum guidelines.
I copied your original question here:
Hi
I need help to find the best snippet to extract individual digits from an integer.
Here is an example of what I want to do:
int number = 1024;
int digit1 = 1;
int digit2 = 0;
int digit3 = 2;
int digit4 = 4;
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
int source
int digit
while source
{
digit = source % 10
output digit
source /= 10
}
get u started
Tadeusz Westawic
Sum quid sum.
|
|
|
|
|
there is no need to start this thread all over again.
My message to the OP was: don't remove what you wrote, which, for the curious, is: "..."
|
|
|
|
|
we call it sprintf .
(Does Arduino provide it?)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
unfortunately that is not an algorithm, nor the name of an algorithm.
however, if it is meant as a suggestion to have a look at an sprintf implementation, it is a valid one, as there are umpteen implementations available; almost any microprocessor vendor or open source kernel offers one.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
Luc Pattyn wrote: unfortunately that is not an algorithm, nor the name of an algorithm.
I know.
Luc Pattyn wrote: almost any microprocessor vendor or open source kernel offers one.
That's the point, there's no need to look at the function implementation details (well, actually there is "curiosity"), my suggestion was: "use sprintf " that is already optimized (the algorithm then becomes trivial, just subtract '0' to every character...).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
isn't sprintf language specific? Unless the OP has very good knowledge of C or in case he is programming in some high level language like python, your answer will be greek to him. Consider explaining the 'optimized' algorithm of sprintf to him instead...
|
|
|
|
|
Wow, now I see the OP convincing Arduino it can run Python code...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
I use C++ to make programs for my arduino
Of course, arduino's library collection includes sprintf() in stdio.h
By the way it is a c++ compiler
|
|
|
|
|
ant-damage wrote: I use C++ to make programs for my arduino
ant-damage wrote: By the way it is a c++ compiler
Did you use C++?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
of course I do
Which programming language could be better to be used along the arduino? C++
|
|
|
|
|
Given two integers, x & y, is it possible to tell a number Z such that Z = f(x,y) and Z is unique for all x,y?
*Edit : f(x) + f(y) --> f(x,y)
modified on Saturday, May 15, 2010 11:04 AM
|
|
|
|
|
I think you mean: is there a function f(x) such that, for different pairs (x,y), the value of f(x)+f(y) will always be different, where x and y are arbitrary integer numbers (negative or positive, and not restricted to a finite domain). Different pairs here has to be taken independent of order.
If that is what you meant, the answer IMO would be no. I'd want you to confirm my interpretation first though.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
sorry, i did not mean to use f(x) + f(y) but f(x,y). my mistake.
so what I want to know is if it is possible to convert 2 numbers into a single unique number so that instead of storing two numbers, I can store just one number. Then afterwards, while reading the stored data, I read one number and apply the inverse of f to get my two numbers x & y.
say for eg. Z = 17*x^2 + y^2
for (x,y) = (3,4)
Z = 17*9 + 16 = 169
to get x,y from 169, possible values will be x = [1,4,9]; equivalent y will then be = [153, 101, 16]
since 16 is the only perfect square, so i get x=3, y=4.
I was just wondering about this problem. It's not required in any practical application. So, you can assume that x,y belongs to a set of ALL POSITIVE INTEGERS only. or better 0<x<=y<10000
And order in which inputs are given does not matter.
I hope I am clear now.
|
|
|
|
|
the easiest possible answer would be
f(x,y) = a*x + y
where a = Maximum possible value of y + 1
|
|
|
|
|
Yes, but the width of the result will be at least as much as the sum of the widths of the inputs (proof below). Which is why every way to do this will suck in practice.
For example you could interleave the bits of the inputs, spreading x to the even bits of the result and y to the odd bits.
Or you could (but this sucks) take the product of the x-th and y-th prime numbers.
(this is not really a formal proof, but close)
Proof that #f(x,y) >= #z+#y
Let #x denote the size of x in bits.
Assume f(x,y) is unique for every pair x,y and #f(x,y) < #z+#y
Then 2^#f(x,y) < 2^(#z+#y) (where ^ denoted exponentiation)
Since there are less possible outcomes than there are inputs, at least one output must correspond to multiple inputs.
f(x,y) is not unique for at least one pair x,y so the assumption must be false.
Therefore #f(x,y) >= #x+#y
Instead of bits you could use any other base, just replace the 2 later on.
modified on Saturday, May 15, 2010 11:33 AM
|
|
|
|
|
No. Both numbers carry an amount of "information", and you need memory ("bits") to store it; as long as there are no restrictions to the values (one number can have a value in some range, and the other can also have an arbitrary value in some range), you need a number of bits to store that information, no matter what reversible transformations you apply; and turning two numbers in one is just a transformation, not necessarily a reversible one.
Therefore, assuming a domain of [0, max-1] then z = x * max + y is as good as it gets.
BTW: having one or a few examples of something that seems to provide a valid approach, in general is inconclusive.
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
|
|
|
|
|
But then the answer is yes, right? You can turn two numbers into one in a unique way.. It's just usually useless since you're still using at least as many bits as the input used to be
|
|
|
|
|