Printing float's to the console
Printing float's to the console
Has anyone ever tried to put a floating point number into a printf? It always gives me garbage, but if a convert it to an integer then it works find. Very annoying when you want to make sure a float is right
Anyone seen a solution?
Use prinf("%f",my_float_value);
Read this:
http://www.rembrandtstr.bocholt.de/man/ ... html#TOC89
to know more about formating in printf() and other similar functions.
Read this:
http://www.rembrandtstr.bocholt.de/man/ ... html#TOC89
to know more about formating in printf() and other similar functions.
Tomasz Nowakowski
Openoko - www.openoko.pl
Openoko - www.openoko.pl
"%f", "%e", "%E", "%g", "%G" all print out floats and "%lf" prints a double
"%d", "%i", "%ld" print out int, int and long int (signed)
"%p" prints the address contained in a pointer
"%c" prints a character
"%s" prints a string
"%o", "%u", "%x", "%X" prints an unsigned integer to octal (%o), decimal (%u) or hex (%x %X). With the lower case x, a-f are lower case, with upper case, a-f are upper case
with floats, you can specify precision (default is 6). So, "%.3f" would print out a float to 3 decimal places.
when using precision with integers it indicates the number of digits to print. If there are less digits than the precision amount, leading zeros will be added.
"%d", "%i", "%ld" print out int, int and long int (signed)
"%p" prints the address contained in a pointer
"%c" prints a character
"%s" prints a string
"%o", "%u", "%x", "%X" prints an unsigned integer to octal (%o), decimal (%u) or hex (%x %X). With the lower case x, a-f are lower case, with upper case, a-f are upper case
with floats, you can specify precision (default is 6). So, "%.3f" would print out a float to 3 decimal places.
when using precision with integers it indicates the number of digits to print. If there are less digits than the precision amount, leading zeros will be added.
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
-Japanese Proverb
-
Midnight
I figuared out how to export a direct x file from blender but I can't get it to display in the mesh viewer it seems to load the file but is either scaled out of sight or the texture is invisable.
what settings do I need in blender to make it visable?
I'm getting an error in the meshviewer console:
error reading .x file something about the header which in the .x file is the texture coords I believe.
any help?
what settings do I need in blender to make it visable?
I'm getting an error in the meshviewer console:
error reading .x file something about the header which in the .x file is the texture coords I believe.
any help?
BCD??? You mean a binary number?Tyn wrote:Can you convert from a hex to a BCD inside the printf function? We could have used that in work the other day rather than working it out on a bit of paper ( although it did mean that the guy above me got to explain what a BCD is which was useful ).
"The reputation of a thousand years may be determined by the conduct of one hour."
-Japanese Proverb
-Japanese Proverb
No, it's how hex numbers are represented in binary. Called Binary Coded Decimal or something simular. On a low level you need it, however the Windows calculator only converts to hex, binary, decimal and Oct. Never heard of it myself until I needed it ( apparently I am going to need to understand and use it a lot ) and it is a real pain in the arse for long hex numbers. It goes:
Each hex digit is 4 bits. The scale in hex is from 0 to 15 so for each digit you get the binary digit for it. 6f ( or 21 ) would equate to:
0110 1111
For the drive I am using in work, it represents this figure as a decimal ( by doing a straight conversion ) on the screen. So it outputs the conversion which is 111.
I need a calculator to take in the BCD number 111 and output either 6f or 21. I wondered if C++ has a funtion to convert this. I couldn't find it when I googled it so I wonder if I have to try and come up with an algorithm to do it ( which I really cannot be arsed to do ). I thought that printf does conversions so I wondered if there is something simular to output this BCD digit into an int.
Each hex digit is 4 bits. The scale in hex is from 0 to 15 so for each digit you get the binary digit for it. 6f ( or 21 ) would equate to:
0110 1111
For the drive I am using in work, it represents this figure as a decimal ( by doing a straight conversion ) on the screen. So it outputs the conversion which is 111.
I need a calculator to take in the BCD number 111 and output either 6f or 21. I wondered if C++ has a funtion to convert this. I couldn't find it when I googled it so I wonder if I have to try and come up with an algorithm to do it ( which I really cannot be arsed to do ). I thought that printf does conversions so I wondered if there is something simular to output this BCD digit into an int.
@Tyn
You are not right about BCD. Read here about what BCD realy is:
http://www.danbbs.dk/~erikoest/bcd.htm
Hex numbers are represented in binary in exacly the same way as numbers in other system (like octal or decimal).
> I need a calculator to take in the BCD number 111 and output either 6f or 21.
21 ? 111 in hex is 6F and only 6F
> I wondered if C++ has a funtion to convert this.
No, but it should be easy to find some third party libraries for this. Look for "hex to binary conversion" or something like that, becasue BCD is not what you think it is. And converting hex to it is a real pain in the ass
> I couldn't find it when I googled it so I wonder if I have to try and come
> up with an algorithm to do it ( which I really cannot be arsed to do ).
Here's some conversion algorithms form hex to other representations:
http://www.danbbs.dk/~erikoest/hex.htm
> so I wondered if there is something simular to output this BCD digit into an int.
All data in computer's memory is stored as binary
You are not right about BCD. Read here about what BCD realy is:
http://www.danbbs.dk/~erikoest/bcd.htm
Hex numbers are represented in binary in exacly the same way as numbers in other system (like octal or decimal).
> I need a calculator to take in the BCD number 111 and output either 6f or 21.
21 ? 111 in hex is 6F and only 6F
> I wondered if C++ has a funtion to convert this.
No, but it should be easy to find some third party libraries for this. Look for "hex to binary conversion" or something like that, becasue BCD is not what you think it is. And converting hex to it is a real pain in the ass
> I couldn't find it when I googled it so I wonder if I have to try and come
> up with an algorithm to do it ( which I really cannot be arsed to do ).
Here's some conversion algorithms form hex to other representations:
http://www.danbbs.dk/~erikoest/hex.htm
> so I wondered if there is something simular to output this BCD digit into an int.
All data in computer's memory is stored as binary
Tomasz Nowakowski
Openoko - www.openoko.pl
Openoko - www.openoko.pl
Actually that page says exactly what I though BCD is.
> 21 ? 111 in hex is 6F and only 6F
Got calculations wrong. Never try to work something out in head after long day at work. Especially when Windows has a calculator.
>Hex numbers are represented in binary in exacly the same way as numbers in other system
Not in this particular piece of kit, from what the guys who come over to England told us was that it was how all hex numbers are stored in memory in all computers, though they were German
Maybe I got the calc wrong somewhere but the effect is that it takes a hex digit and converts it to binary, then adds it up. On the screen you will get an exact match for smaller numbers but you definatly don't get the same for larger figures. Maybe I got the calculation wrong somewhere in there but I know it works because I have seen it. I have a test drive at work, I will do see what it spits out and post back the results when I get some free time.
I can tell you for definate that a hex conversion to decimal is NOT the same as a hex to BCD conversion. I know this through thinking values were decimal, using them as decimals because they looked about right but they were actually wrong. It is also not the same as hex to binary as there are seperate functions to do this as well. The problem is that the amount of memory is so small that there simply isn't space for me to be doing conversion so I can be looking at decimal figures instead of BCD numbers.
They appear in the same format as decimal but are not quite the same, it can throw you. Has me many times in the last few weeks.
> All data in computer's memory is stored as binary
Not in the way it was represented here. They look identical to an integer but the value is sometimes different ( more so with larger numbers from what I have seen ). I don't remember exactly how they said to work it out, I thought it was the way I explained before but since I screwed up the hex convertion in the first place it probably isn't that
For an example, though certainly not accurate as these are estimates, I had a BCD of around 6000. It worked out to be a decimal value of 3000 or something along those lines.
_____________________
I think I found out what I get it from. I think it was that you if you had:
6f
Then that would equal 111 as a decimal. As a BCD ( or at least the way it was described there ) it would be stored as 8 bits, 4 for each digit. So:
6 = 0110
F = 1111
If put along side each other you would get 111, or:
111 = 01101111
But that's not how BCD is represented. In the way BCD is represented ( at least in this kit I was using ) you add the two together to get:
0110 + 1111 = 10101
Or 21.
> 21 ? 111 in hex is 6F and only 6F
Got calculations wrong. Never try to work something out in head after long day at work. Especially when Windows has a calculator.
>Hex numbers are represented in binary in exacly the same way as numbers in other system
Not in this particular piece of kit, from what the guys who come over to England told us was that it was how all hex numbers are stored in memory in all computers, though they were German
Maybe I got the calc wrong somewhere but the effect is that it takes a hex digit and converts it to binary, then adds it up. On the screen you will get an exact match for smaller numbers but you definatly don't get the same for larger figures. Maybe I got the calculation wrong somewhere in there but I know it works because I have seen it. I have a test drive at work, I will do see what it spits out and post back the results when I get some free time.
I can tell you for definate that a hex conversion to decimal is NOT the same as a hex to BCD conversion. I know this through thinking values were decimal, using them as decimals because they looked about right but they were actually wrong. It is also not the same as hex to binary as there are seperate functions to do this as well. The problem is that the amount of memory is so small that there simply isn't space for me to be doing conversion so I can be looking at decimal figures instead of BCD numbers.
They appear in the same format as decimal but are not quite the same, it can throw you. Has me many times in the last few weeks.
> All data in computer's memory is stored as binary
Not in the way it was represented here. They look identical to an integer but the value is sometimes different ( more so with larger numbers from what I have seen ). I don't remember exactly how they said to work it out, I thought it was the way I explained before but since I screwed up the hex convertion in the first place it probably isn't that
For an example, though certainly not accurate as these are estimates, I had a BCD of around 6000. It worked out to be a decimal value of 3000 or something along those lines.
_____________________
I think I found out what I get it from. I think it was that you if you had:
6f
Then that would equal 111 as a decimal. As a BCD ( or at least the way it was described there ) it would be stored as 8 bits, 4 for each digit. So:
6 = 0110
F = 1111
If put along side each other you would get 111, or:
111 = 01101111
But that's not how BCD is represented. In the way BCD is represented ( at least in this kit I was using ) you add the two together to get:
0110 + 1111 = 10101
Or 21.
Well, Tyn, read this again attentively and think about it!Tyn wrote:No, it's how hex numbers are represented in binary. Called Binary Coded Decimal or something simular.
It's not how hex numbers are represented in binary.
It's how decimal numbers are represented in binary.
As the name implies -> "Binary Coded Decimal".
Otherwise it would be called Binary Coded Hexadecimals (BCH)...
Converting Hex to BCD doesn't really make sense, does it? If at all you would convert hex to decimal first and then to BCD... maybe...
Not the way it was explained to me. It is a Hex represented as a decimal but stored as a binary number. The hex is stored as a binary instead of the decimal being stored as a binary, which makes a difference to what it reads as if you tried to convert what was physically in the memory as a decimal. So it does make sense
Whether this is some system that the people who designed the kit have I don't know but I can tell you that it is what they told me and that the BCD's are represtative of the Hex numbers, not a straight conversion from Hex to Decimal or Binary. From what I got from them, you take each unit or digit of the hex, get it's binary equivilent and add them together ( I think, I will check this tomorrow ). They store it in 4 bits per unit as the maximum figure will be 15. They then use the sum of the units and convert it to a decimal.
At the time it made sense to me but now you two have cast some shadow onto that certainty ( not a bad thing, I'd rather not completely understand than believe something that isn't true ).
Having never done serious programming before, always been a hobby more than anything, being asked to do it at work is a leaning process so as you can accept I have to trust what I am told about things like this because I have never run into it before. They said that there is a difference and I can see that the BCD number is like a decimal but when converted to a decimal you get a different figure, so there is something going on there. If you two know more than me about what could be going on then I'd definatly want to know as it seems like something that I should understand.
The problem is that the kit I am using is very cheap, therefore operates on a very low level. My company likes cheap kit which means I have to know what these things mean.
Whether this is some system that the people who designed the kit have I don't know but I can tell you that it is what they told me and that the BCD's are represtative of the Hex numbers, not a straight conversion from Hex to Decimal or Binary. From what I got from them, you take each unit or digit of the hex, get it's binary equivilent and add them together ( I think, I will check this tomorrow ). They store it in 4 bits per unit as the maximum figure will be 15. They then use the sum of the units and convert it to a decimal.
At the time it made sense to me but now you two have cast some shadow onto that certainty ( not a bad thing, I'd rather not completely understand than believe something that isn't true ).
Having never done serious programming before, always been a hobby more than anything, being asked to do it at work is a leaning process so as you can accept I have to trust what I am told about things like this because I have never run into it before. They said that there is a difference and I can see that the BCD number is like a decimal but when converted to a decimal you get a different figure, so there is something going on there. If you two know more than me about what could be going on then I'd definatly want to know as it seems like something that I should understand.
The problem is that the kit I am using is very cheap, therefore operates on a very low level. My company likes cheap kit which means I have to know what these things mean.
Such confusion over BCD. I've worked with BCD before as I aslo do low-level electronics. However, I don't think I've used more than 4 bits at a time,all I can recall is a BCD-to-7-segment-decoder chip(7448 I think) for driving a numeric LED.
So adding the BCD digits surprised me. To convert hex to BCD I'd convert the hex to binary(easy) then convery the binary to BCD (not too hard. I wrote a function to do it a few minutes ago out of curiosity). As withwhat you finally got, I'd get 6F hex= 0110, 1111 as 2-digit BCD. Adding them together surprises me, but if that's what the company said it's probably right, at least for some applications. If you had a number of 7448 BCD-to-7-seg's you would not want to add your BCD digits or you would get totally the wrong numeric output.
Also, looking strictly at the phrase "binary coded decimal" it does not make sense to add them. It would make the most sense if each decimal digit were simply expanded into 4 binary digits. In my view, binary coded decimal should simply be a way of representing decimal digits in binary. If you add the binary representations of the digits together how will you ever convert them back into decimal, hex, or anything meaningful?
0110 + 1111 =10101 but 0111 + 1110 also =10101
sorry for going on, but the longer i type the more i think that adding the BCD digits makes no sense
So adding the BCD digits surprised me. To convert hex to BCD I'd convert the hex to binary(easy) then convery the binary to BCD (not too hard. I wrote a function to do it a few minutes ago out of curiosity). As withwhat you finally got, I'd get 6F hex= 0110, 1111 as 2-digit BCD. Adding them together surprises me, but if that's what the company said it's probably right, at least for some applications. If you had a number of 7448 BCD-to-7-seg's you would not want to add your BCD digits or you would get totally the wrong numeric output.
Also, looking strictly at the phrase "binary coded decimal" it does not make sense to add them. It would make the most sense if each decimal digit were simply expanded into 4 binary digits. In my view, binary coded decimal should simply be a way of representing decimal digits in binary. If you add the binary representations of the digits together how will you ever convert them back into decimal, hex, or anything meaningful?
0110 + 1111 =10101 but 0111 + 1110 also =10101
sorry for going on, but the longer i type the more i think that adding the BCD digits makes no sense
You do a lot of programming? Really? I try to get some in, but the debugging keeps me pretty busy.
Crucible of Stars
Crucible of Stars