Octal Numbering System - octal

In octal, I'm typing the number 10.34 into a couple of different online converters to convert them to decimal, and they say that it's an invalid octal number.
I know that the octal number 10 can be worked out by
1x8^1 + 1x8^0 = 8
But is it possible to workout the right side of the radix.

Easy. Just continue the pattern from the left side of the dot:
1*8^1 + 0*8^0 + 3*8^-1 + 4*8^-2 = 8.4375

Related

how does hexadecimal to decimal work in swift? [duplicate]

I don't understand how floating point numbers are represented in hex notation in Swift. Apple's documentation shows that 0xC.3p0 is equal to 12.1875 in decimal. Can someone walk me through how to do that conversion? I understand that before the decimal hex value 0xC = 12. The 3p0 after the decimal is where I am stumped.
From the documentation:
Floating-Point Literals
...
Hexadecimal floating-point literals consist of a 0x prefix, followed
by an optional hexadecimal fraction, followed by a hexadecimal
exponent. The hexadecimal fraction consists of a decimal point
followed by a sequence of hexadecimal digits. The exponent consists of
an upper- or lowercase p prefix followed by a sequence of decimal
digits that indicates what power of 2 the value preceding the p is
multiplied by. For example, 0xFp2 represents 15 × 22, which evaluates
to 60. Similarly, 0xFp-2 represents 15 × 2-2, which evaluates to 3.75.
In your case
0xC.3p0 = (12 + 3/16) * 2^0 = 12.1875
Another example:
0xAB.CDp4 = (10*16 + 11 + 12/16 + 13/16^2) * 2^4 = 2748.8125
This format is very similar to the %a printf-format (see for example
http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html).
It can be used to specify a floating point number directly in its
binary IEEE 754 representation, see Why does Swift use base 2 for the exponent of hexadecimal floating point values?
for more information.
Interpret 0xC.3p0 using the place value system:
C (or 12) is in the 16^0 place
3 is in the 16^-1 place (and 3/16 == 0.1875)
p says the exponent follows (like the e in 6.022e23 in base 10)
0 is the exponent (in base 10) that is the power of 2 (2^0 == 1)
So putting it all together
0xC.3p0 = (12 + (3/16)) * 2^0 = 12.1875
In order to sum up what I've read, you can see those representations as follow:
0xC.3p0 = (12*16^0 + 3*16^-1) * 2^0 = 12.1875
From Martin R's example above :
0xAB.CDp4 = (10*16^1 + 11*16^0 + 12*16^-1 + 13*16^-2) * 2^4 = 2748.8125
The 0xC is 12, as you said. The decimal part is ((1/16)*3)*10^0.
So you need to take the decimal part and divide it by 16. Then you need to multiply it by 2 raised to the power of the number after the p
Hexadecimal -(0-9,A=10,B=11,C=12,D=13,E=14,F=15) and p0 means 2^0
ex: - 0xC = 12 (0x prefix represents hexadecimal)
After the decimal part as in 0xC.3p0 we divide the numbers with the power of 16
So here its 3/16 = 0.1875
so 0xC.3p0 = (12 + (3/16) ) 2^0
If it was 0xC.43p0 then for the 4 we would use 4/(16), for 3 we would use 3/(16 ^2) and similarly if the decimal part increases.
ex: 0xC.231p1 = (12 + 2/16 + 3/(256) + 1/(16^3)) 2^1 = 24.27392578125

Add leading and trailing zeroes to a string/label in swift

In swift I know how to set the number of digits after the decimal point when converting a double to a string:
String(format: "%0.2f", someDouble)
Similarly I know how to set the number of digits before the decimal point:
String(format: "%02d", someDouble)
But how can I do both?
I want the string to always have a 00.00 format.
Thanks
You simply combine the two:
String(format: "%05.2f", someDouble)
The 0 means fill with leading zeros as needed.
The 5 means you want the final output to be at least 5 characters, include the decimal point.
The .2 means you want two decimal places.
If this is a number you are showing to a user then you should probably use NumberFormatter so the decimal is properly formatted for the user's locale.

dot notation with p for hexadecimal numeric literals in swift

I'm working through the first basic playground in https://github.com/nettlep/learn-swift using XCode
What exactly is happening with this expression?
0xC.3p0 == 12.1875
I've learned about hexadecimal literals and the special "p" notation that indicates a power of 2.
0xF == 15
0xFp0 == 15 // 15 * 2^0
If I try 0xC.3 I get the error: Hexadecimal floating point literal must end with an exponent.
I found this nice overview of numeric literals and another deep explanation, but I didn't see something that explains what .3p0 does.
I've forked the code and upgraded this lesson to XCode 7 / Swift 2 -- here's the specific line.
This is Hexadecimal exponential notation.
By convention, the letter P (or p, for "power") represents times two
raised to the power of ... The number after the P is decimal and
represents the binary exponent.
...
Example: 1.3DEp42 represents hex(1.3DE) × dec(2^42).
For your example, we get:
0xC.3p0 represents 0xC.3 * 2^0 = 0xC.3 * 1 = hex(C.3) = 12.1875
where hex(C.3) = dec(12.{3/16}) = dec(12.1875)
As an example, you can try 0xC.3p1 (equals hex(C.3) * dec(2^1)), which yields double the value, i.e., 24.375.
You can also study the binary exponent growth in a playground for hex-value 1:
// ...
print(0x1p-3) // 1/8 (0.125)
print(0x1p-2) // 1/4 (0.25)
print(0x1p-1) // 1/2 (0.5)
print(0x1p1) // 2.0
print(0x1p2) // 4.0
print(0x1p3) // 8.0
// ...
Finally, this is also explained in Apple`s Language Reference - Lexical Types: Floating-Point Literals:
Hexadecimal floating-point literals consist of a 0x prefix, followed
by an optional hexadecimal fraction, followed by a hexadecimal
exponent. The hexadecimal fraction consists of a decimal point
followed by a sequence of hexadecimal digits. The exponent consists
of an upper- or lowercase p prefix followed by a sequence of decimal
digits that indicates what power of 2 the value preceding the p is
multiplied by. For example, 0xFp2 represents 15 x 2^2, which
evaluates to 60. Similarly, 0xFp-2 represents 15 x 2^-2, which
evaluates to 3.75.

How do I read last 20 digits of string in swift

I have a swift program in whom I need to read the last 20 digits of a string.
Although I would prefer the last 20 digits the first 20 would also be fine if it makes it any easier.
And a way to read all Digits except for the last 20.
You can use suffix:
String(yourString.characters.suffix(20))
It's interesting because the place you'd expect to find the answer would be the string functions -- where is the Swift equivalent of Javascript's String.substr() for example.
What you want is
String str = ...
str.substringFromIndex(advance(str.startIndex, 20)) // first 20 chars
str.substringFromIndex(advance(str.endIndex, -20)) // last 20 chars
In any case, you'll need to check if the str has fewer than 20 characters and just return the string itself.
You can determine the string length by
count(str) (older Swift versions) or str.characters.count (Swift 1.2)

Floating point hex notation in Swift

I don't understand how floating point numbers are represented in hex notation in Swift. Apple's documentation shows that 0xC.3p0 is equal to 12.1875 in decimal. Can someone walk me through how to do that conversion? I understand that before the decimal hex value 0xC = 12. The 3p0 after the decimal is where I am stumped.
From the documentation:
Floating-Point Literals
...
Hexadecimal floating-point literals consist of a 0x prefix, followed
by an optional hexadecimal fraction, followed by a hexadecimal
exponent. The hexadecimal fraction consists of a decimal point
followed by a sequence of hexadecimal digits. The exponent consists of
an upper- or lowercase p prefix followed by a sequence of decimal
digits that indicates what power of 2 the value preceding the p is
multiplied by. For example, 0xFp2 represents 15 × 22, which evaluates
to 60. Similarly, 0xFp-2 represents 15 × 2-2, which evaluates to 3.75.
In your case
0xC.3p0 = (12 + 3/16) * 2^0 = 12.1875
Another example:
0xAB.CDp4 = (10*16 + 11 + 12/16 + 13/16^2) * 2^4 = 2748.8125
This format is very similar to the %a printf-format (see for example
http://pubs.opengroup.org/onlinepubs/009695399/functions/fprintf.html).
It can be used to specify a floating point number directly in its
binary IEEE 754 representation, see Why does Swift use base 2 for the exponent of hexadecimal floating point values?
for more information.
Interpret 0xC.3p0 using the place value system:
C (or 12) is in the 16^0 place
3 is in the 16^-1 place (and 3/16 == 0.1875)
p says the exponent follows (like the e in 6.022e23 in base 10)
0 is the exponent (in base 10) that is the power of 2 (2^0 == 1)
So putting it all together
0xC.3p0 = (12 + (3/16)) * 2^0 = 12.1875
In order to sum up what I've read, you can see those representations as follow:
0xC.3p0 = (12*16^0 + 3*16^-1) * 2^0 = 12.1875
From Martin R's example above :
0xAB.CDp4 = (10*16^1 + 11*16^0 + 12*16^-1 + 13*16^-2) * 2^4 = 2748.8125
The 0xC is 12, as you said. The decimal part is ((1/16)*3)*10^0.
So you need to take the decimal part and divide it by 16. Then you need to multiply it by 2 raised to the power of the number after the p
Hexadecimal -(0-9,A=10,B=11,C=12,D=13,E=14,F=15) and p0 means 2^0
ex: - 0xC = 12 (0x prefix represents hexadecimal)
After the decimal part as in 0xC.3p0 we divide the numbers with the power of 16
So here its 3/16 = 0.1875
so 0xC.3p0 = (12 + (3/16) ) 2^0
If it was 0xC.43p0 then for the 4 we would use 4/(16), for 3 we would use 3/(16 ^2) and similarly if the decimal part increases.
ex: 0xC.231p1 = (12 + 2/16 + 3/(256) + 1/(16^3)) 2^1 = 24.27392578125