View Full Version : A method of convert decimal to binary and binary to decimal
luquinhas0021
16th March 2016, 22:58
First, let's see an example that show us how much the representation in fixed-point and floating point (32 bits, on below example) is not so good (And the "not so good" is applied to many other cases):
0.77 (Basis 10) = 0.11000101000111101011100... (Basis 2)
Like you've seen, the conversion from decimal to binary creates a binary number with infinite number of algarisms. The reconversion to decimal, logically, will not generates the original decimal number, because this binary number is infinite, but we will have cut it somewhere.
Of course, the irrational decimal numbers and the decimal periodic numbers (Like 0.393939...) have to be cutted somewhere. The first, in a maximum available memory disponibility point, rounding it, if necessary; the second one, in a point that the number is still periodic (Like 0.393939, not 0.39393).
Let a.b (a is any integer (With no point) number (Negative or positive), includding "0"; b is bigger or equal to 0.00000000000000000000000000000000 and less or equal than 0.99999999999999999999999999999999...) a real number in basis 10.
What if we can representate any finite decimal number by a finite binary number? I was think about a way to do this...
Conversion from decimal to binary:
let a.b;
read it signal,
put, in signal, bit 0 if a.b is positive or bit 1 if a.b is negative;
convert a.b to BCD 4 bits,
if signal be - (1100), change it for + (1011), in BCD 4 bits
delete the "0"'s (0000) in extreme right, after point, if it exists (Includding if a.b is, in decimal basis, is a.00000000000...),
shift the point (.) (1101) to the extreme right of all packets of BCD 4 bits, if necessary
count the number of shifts by packets BCD 4 bits that the packet 1101 did, and multiplicate it by 11 (-1, in basis 10). The number of shifts will be called "exponent" (exp) ("exponent" will ever be a number of form -x.00000000000..., in basis 10, with x being a natural number from 0 to infinite),
the generated number by n shifts will be called "integer". ("integer" will ever be a number of form y.00000000000..., in basis 10, with y being a natural number from 0 to infinite). Convert it to binary representation of natural numbers.
Conversion from binary to decimal:
read the exponent,
multiply it by 11 (-1, in basis 10);
convert the integer part to decimal,
put the 1101 packet between the, from right to left, the o + 1 and o in the integer ("o" is the number that is in exponent part);
read the signal,
put it in extreme left of number generated in fourth step: 1011, if bit of signal be 0, or 1100, if bit of signal be 1;
convert the result to decimal basis.
Thus, the representation will be:
(Bit for "signal") (Bit(s) for "integer") (Bits for "exponent")
How the "signal" will ever have only 1 bit, but "integer" and "exponent" will have one or more than 1 bit, I was think about make three separable packets of this number...
Packet I: (Bit for "signal")
Packet II: (Bit 0 and ((Bit(s) for "integer"))
Packet III: (Bit 1 and (Bit(s) for "exponent"))
No number will have more than 1 packet I, neither more than 1 packet II, neither than more than 1 packet III. If doesn't possible to make packets, will be maden like floating point numbers: define number of bits for each of two last parts (integer" and "exponent"), according to interval of binary numbers with same number of bits.
As you can see, this method can represent any real number Theoretically, even any infinite number).
This form of representation isn't good in numbers perfectly represented by fixed-point or floating point method (Finite binary number), because, compared with the two last mentioned methods, in some cases, this my method creates a binary number with more bits.
Any binary number, in fixed-point or floating point representation, can be converted to my representation.
If you want examples, I will give us. Just ask me for.
I didn't write the arithmetical operations because the post would stay so big, and I don't know if you will like my methods.
This my method is designed, about representations and operations, for absolute numeric precision.
wonkey_monkey
17th March 2016, 00:32
As you can see, this method can represent any real number
Can it represent pi?
What's the point of all this? Does it solve a problem? Where are the decimals you're so desparate to process in this way even coming from in the first place?
Honestly, I'm making the ways of do the arithmetical operations just for the first method (I will write about it after), because, although possible, the arithmetical operations of second method are slow.
That's probably because computers are absoltely awful at anything other than binary arithmetic, so of course forcing them to operate on pseudo-decimals is going to slow things down. Why do you even want to do this?
Finally: what does any of this have to do with video processing?
raffriff42
17th March 2016, 02:07
You seem to be talking about binary-coded decimal
https://en.wikipedia.org/wiki/Binary-coded_decimal
There are a number of fine ways of getting arbitrary-precision arithmetic
https://en.wikipedia.org/wiki/List_of_arbitrary-precision_arithmetic_software
I would probably look at the Boost multiprecision library first
http://www.boost.org/doc/libs/1_60_0/libs/multiprecision/doc/html/index.html
Or quadruple-precision. After all, 128 bits ought to be enough for anybody
https://en.wikipedia.org/wiki/Quadruple-precision_floating-point_format
luquinhas0021
17th March 2016, 20:58
davidhorman, as I already told you, both of my two methods can represent any real numbers; theoretically, even the infinite ones. So, theoretically, "pi" can be represented.
I don't know if you realize the utility of my two methods. But, for instance, I will give us one example:
Do you remember how many bits, in fixed-point and floating point, are needed for representating an APROXIMATION of 0.77?
Ben, now, I will show you how representate it in my system.
Packet I: 0
Packet II: 01001011
Packet III: 1110
(The packet III, whatever be the exponent, ever will have two bits 1: the first, left, is the indicative to "exponent"; the second is the indicative to negative signal.
Hm... Better, no?
raffriff42
17th March 2016, 22:30
So, theoretically, "pi" can be represented.With less than infinite digits (binary, decimal or any other number system)?
Wow, that's amazing ----- if true.
https://en.wikipedia.org/wiki/Irrational_number
wonkey_monkey
18th March 2016, 00:08
So, theoretically, "pi" can be represented.
As riffraff42 has said, not without an infinite number of digits it can't. Which is the same in binary, decimal, hexadecimal, or any other base (except base pi, I suppose)
I don't know if you realize the utility of my two methods.
I think you accidentally a letter...
But, for instance, I will give us one example:
Do you remember how many bits, in fixed-point and floating point, are needed for representating an APROXIMATION of 0.77?
...
Hm... Better, no?
Well... no, not really. Not unless you tell us what you need perfect decimal precision for. It's certainly not necessary for video processing, which doesn't usually require decimal input from humans (or if it does, the floating point approximation is usually perfectly adequate).
I can pretty much guarantee that any applicable problems have already been catered for using methods developed decades ago. SQL has had the decimal() type for quite a while, for example, and riffraff42 has given other examples.
feisty2
18th March 2016, 05:10
stuff like Matlab Maple and Mathematica got the ability to do what you want
They were delicately designed to do mathematically perfect calculations (error = 0, precision loss will never ever happen)
Even calculations involving irrational numbers like 1+pi, will stay the 1+pi kind of infinite precision form, not 4.1415926... in those programs
So, you already have what you want
Or you can implement the Matlab like mathematically perfect calculation in your own program
colours
18th March 2016, 23:56
davidhorman, as I already told you, both of my two methods can represent any real numbers; theoretically, even the infinite ones. So, theoretically, "pi" can be represented.
A number having an infinite-length representation is not the same as the number being infinite. Map, territory, etc. (https://en.wikipedia.org/wiki/Map%E2%80%93territory_relation)
Also, counting argument: there are uncountably many reals (https://en.wikipedia.org/wiki/Cantor's_diagonal_argument) and countably many finite binary strings, so it's impossible to biject between them. There will always be some real number you cannot represent finitely.
First method, using packets:
Packet I: 0
Packet II: 01001011
Packet III: 1110
(The packet III, whatever be the exponent, ever will have two bits 1: the first, left, is the indicative to "exponent"; the second is the indicative to negative number)
Second method:
0.1001011
Hm... Better, no?
No, because now you're effectively working over an alphabet of five symbols: "−", "0", "1", "." and an end marker. As a variable-length code without an in-band termination signal, the end marker is not optional. IEEE 754 floats have a fixed length and are not affected by this problem. This also needs over a thousand bits to represent a number like 2^−1000, which IEEE 754 double precision handles in just sixty-four bits.
There are different pros and cons for different ways of representing a number, and it is generally safe to assume that whatever is in mainstream usage for a specific situation is in mainstream usage because the pros outweigh the cons.
luquinhas0021
19th March 2016, 14:39
Feisty, how Matlab can make calculations with error = 0?
LoRd_MuldeR
19th March 2016, 14:54
Feisty, how Matlab can make calculations with error = 0?
By operating on mathematical terms instead of (approximated) numbers – where possible.
If, for example, you enter "sqrt(2)", we can simply store the term "sqrt(2)" instead of computing a numerical approximation of its value (1.414213562373...). While any approximation on a real computer (i.e. with a limited number of bits) will unavoidably contain some approximation error, the term itself is as accurate as it can be. Later, when several terms are combined, in one way or another, it is also possible to use calculation rules in order to strip out (or simplify) those mathematical terms – where possible. This way, an accurate end results can be computed – even if some of the intermediate results can not numerically be represented accurately with a finite number of bits...
wonkey_monkey
19th March 2016, 19:18
it is also possible to use calculation rules in order to strip out (or simplify) those mathematical terms – where possible. This way, an accurate end results can be computed
Ah, but there's that caveat of "where possible". Where not possible, some error must still be induced.
I can't see how it would be possible, for example, to calculate pi*sqrt(2)*e without specifying a limit to precision first. At some point, it's going to have to do one multiplication or the other first, and store an intermediate result.
luquinhas0021
21st March 2016, 20:07
"You seem to be talking about binary-coded decimal" (davidhorman)
No, there's nothing to do this, unless (I'm thinking) in number input, if have it, when the number that user will type will be represented in BCD 4 bits (0000 to 1011 (From 0000 to 1001 (1 to 9) and 1011 for +, 1100 for - and 1101 for . (Point)), instead to be converted to FP256, as I told in first post. Then, the point (1011) will be shifted, if necessary, to the extreme right of "number". Then, this will be converted to binary as itself. The use of BCD is cheaper than convert to FP256... And don't give us any error! I'm only worry with conversion from binary to BCD 4 bits (Someone here know how it is done?), especially in fixed-point and floating point (Do you remember that my system can work with any binary representation, as long as the other representation be converted to decimal representation?). This conversion (Binary to BCD 4 bits) will be good for rounding the numbers, if needed, as it is done in decimal basis, of a way that is conditional to each case.
I've read here - wasn't only one time - the following: it can represent "pi"? It can represent a number with less than infinite digits? there numbers that cannot be represented by finite number of binary digits... Ben, let's go:
Be "n" a binary number, composed of bit of signal, bit (s) of natural part and bits of exponent. The signal will have a bit 0 (+) or 1 (-); the natural part, in basis 10, will be a natural number ((+) a.000000000000000000000...), and the exponent, in basis 10, will be 0 or whatever negative natural number, of way (-) a.000000000000000000000...
Ben... There's some natural number, from 0 to positive infinite, that isn't perfectly represent by a binary number? No.
So, theoretically, my system can represent ANY real number, in basis 10, with any number of algarisms. In practice, there's only one limitation: the computer memory.
What about floating point numbers? Even with infinite precision, it wouldn't never represent, without error, 0.79, for example.
If you couldn't realize the superioty of my system over the two I spoke about, I don't know what I will tell...
LoRd_MuldeR
21st March 2016, 23:34
I can't see how it would be possible, for example, to calculate pi*sqrt(2)*e without specifying a limit to precision first. At some point, it's going to have to do one multiplication or the other first, and store an intermediate result.
You don't need to specify limit to precision first here. If a term like "pi*sqrt(2)*e" cannot be simplified any further, using algebraic rules, then "pi*sqrt(2)*e" simply is your end result :)
Of course, at that point, you can still ask the program to give you a numeric approximation of the end result - at whatever precision your want - if that's what you need. Or a you can ask the program for a series representation of the end result.
Example:
https://www.wolframalpha.com/input/?i=pi*sqrt%282%29*e
(Still there exist some mathematical problems for which no analytic solution is known yet, or the analytic solution just is impractical. So, in that case, computing an approximated solution, e.g. by using an iterative algorithm, is the way to go. But then again, you can usually get whatever precision you like, just by doing enough iterations of the algorithm)
StainlessS
22nd March 2016, 01:41
luquinhas0021,
You repeatedly seem to want to make some big impact on the world with your brilliance,
unfortunately you fail also repeatedly.
Your grasp of mathematics is way beyond mine, but in some of your claims, I can only look and shake my head,
you seem not to grasp the basics, take your time and look at everything a little more carefully before you post such stuff,
otherwise you run the risk of everybody thinking that you are an idiot.
(I know about that, most on D9 think I am an idiot, but thing is I really dont care what they think), I am a self confessed idiot.
luquinhas0021
22nd March 2016, 03:40
I won't comment your answer, StainlessS, because it is so... Idiot. Otherwise (If I answer you) I will upset you.
No, I will, yes, answer you answer. If no, I will be bothered:
"You repeatedly seem to want to make some big impact on the world with your brilliance,"
I really don't know what would be the impact of a proposed binary representation on the world. I may say it will reduce the necessary number of bits for representating a number that isn't perfectly represented neither in fixed-point neither in any floating point precision, and I may say too in this system, in finite numbers, there's never error. But, if you actually meet some material of computer numerical representation, you will agree with me that BCD system do the two things I've talked about; the difference is that the aritmetical operation on my system are more simple than in BCD, mainly a x b, a : b, a ^ b and aroot(b), and (I won't compare with BCD) there's no error, there's only, if needed, cut somewhere.
"Your grasp of mathematics is way beyond mine"
"you seem not to grasp the basics"
If I don't know the basics, I'm imagining what you know... Well, my asking makes sense at the point from you didn't can realize what really this system means (For example):
-0.548927361 (Unrepresentable, exactly, in fixed-point and in any precision of floating point) = (-1) x 548927361 x 10 ^ -9
After all steps I've mentioned in first post...
- (Basis 10) = 1 (Basis 2)
548927361 (Basis 10) = 100000101101111111011110000001 (Basis 2)
-9 (Basis 10) = 11001 or 0111 (The last, in complement of two) (Both of two in basis 2)
There's no number of form +-a.000000000... that is represented with error in binary, so...
Logically, the aritmetical operations of this system will be based in ones of the decimal basis..
By last, but not less important: I ain't worried about if this or that application needs or not needs an absolute precision (Zero error). The only thing I'm considering is: how implement my system. End point.
wonkey_monkey
22nd March 2016, 20:42
What about floating point numbers? Even with infinite precision, it wouldn't never represent, without error, 0.79, for example.
FP would represent 0.79 to exactly the same accuracy that your system would represent pi, given infinite precision.
there's no error, there's only, if needed, cut somewhere.
A cut (truncation) is an error. It's exactly the same kind of error as the one you get when you try to represent 0.77 as a float (the fact that you'd need infinite precision to ultimately do so is irrelevant; it's still a truncation).
If you couldn't realize the superioty of my system over the two I spoke about, I don't know what I will tell...
It's superior only for the very narrow definition of superiority you've chosen to use. Yes, your system can repesent 0.77 precisely. But this simply isn't the grand accomplishment you are making it out to be. <i>No-one</i> in computer science circles is going to be surprised or interested in your idea. Pursue it for your own pleasure, by all means, but try to reign in your delusions of grandeur until you know a little more about the subject.
luquinhas0021
22nd March 2016, 21:25
davidhorman,
"FP would represent 0.79 to exactly the same accuracy that your system would represent pi, given infinite precision."
I'm interested to know if the result of calculus of pi is equal or different than the result (Pi) in FP. I will implement for knowing about this.
"A cut (truncation) is an error."
Ben, the unicque error generated by my system. Even that, is more accurated than FP and fixed-point.
"But this simply isn't the grand accomplishment you are making it out to be. Noone in computer science circles is going to be surprised or interested in your idea."
I don't want that the people get surprised with a simply deduced thing.
Seems like you have a capacity of predict the future. But you are being generalist and deprecative. This is an ugly behavior.
Ben, I will implement it by myself... And I doubt that, when the system are ready, noone will wish use it. Maybe everyone doesn't interest, but, at least, yes, one will interest.
Anyway, I was think good you have understood my idea.
Ah, this can be implemented in Avisynth, for maximizing the precision of calculus. As it seems, the highest precision, correct me if I'm wrong, is given by FP16 (Dither_16).
I've updated the first post: I delete a small error there. And the post before this: I've added a little information.
colours
23rd March 2016, 07:22
Ah, this can be implemented in Avisynth, for maximizing the precision of calculus. As it seems, the highest precision, correct me if I'm wrong, is given by FP16 (Dither_16).
HELLO FEISTY2
On a serious note: Dither uses 16-bit integers, not 16-bit floats. And there has been practically no need for any higher precision—16-bit ints are enough.
Yes, there are edge cases where having even higher precision helps. They're also exceedingly rare and not generally useful in the same way 16-bit processing is.
Seems like you have a capacity of predict the future. But you are being generalist and deprecative. This is an ugly behavior.
We are telling you that, firstly, what you're proposing doesn't seem to be novel in any way, and secondly, it is not an improvement over what is in mainstream usage.
Stop assuming you're on to some major breakthrough in image processing and adopt some goddamn humility. Instead, you choose to make a personal attack on someone who has been patiently trying to explain to you what about your idea doesn't work—now that is ugly behaviour.
feisty2
23rd March 2016, 09:46
HELLO FEISTY2
:confused::confused::confused:
how do I have anything to do with this?
are you suggesting that this luq..whatever guy is doing maybe something that I did?
I couldn't care less about new methods of number representation or stuff, I will go matlab-ing if I want infinite precision
or are you suggesting that I signed up an alternative account, this l.. user to just what, play dumb?
I'm a college nerd and I'm all fucked up busy and really just ain't got no fucking time to do boring and low and meaningless things like this, I got better ways of pass-time
I WANT AN APOLOGY
oh, and by the way thanks for ruining my already hell of a day by being mean to me, an irrelevant guy to fill up your burning desire of viciousness
Groucho2004
23rd March 2016, 10:27
:confused::confused::confused:
how do I have anything to do with this?
are you suggesting that this luq..whatever guy is doing maybe something that I did?
I couldn't care less about new methods of number representation or stuff, I will go matlab-ing if I want infinite precision
or are you suggesting that I signed up an alternative account, this l.. user to just what, play dumb?
I'm a college nerd and I'm all fucked up busy and really just ain't got no fucking time to do boring and low and meaningless things like this, I got better ways of pass-time
I WANT AN APOLOGY
Don't get your knickers in a twist.
I think that this was just supposed to be a mildly sarcastic stab at some peoples' obsession with fp precision in video processing. Since you're already feisty2, this should probably have read "feisty3" or "feisty2.1".
Settle down, you're being way too paranoid.
feisty2
23rd March 2016, 10:37
Don't get your knickers in a twist.
I think that this was just supposed to be a mildly sarcastic stab at some peoples' obsession with fp precision in video processing. Since you're already feisty2, this should probably have read "feisty3" or "feisty2.1".
Settle down, you're being way too paranoid.
okay, maybe I'm being paranoid since I just had a real bad day, I will apologize when colours is online and clarified his statement.
pie
23rd March 2016, 11:45
This thread reminds me of this one https://forum.doom9.org/showthread.php?t=171744
Caroliano
29th March 2016, 15:16
In addition to the binary-coded decimal link that raffriff42 posted earlier, there is also:
https://en.wikipedia.org/wiki/Decimal_floating_point
I wish it was used more, in scripting languages for example. More human friendly. Lack of hardware support outside mainframes is the main reason for it's unpopularity. It is also intrinsically less efficient than binary floating point.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.