double precision in MATLAB - matlab
So double precision takes 64 bits in MATLAB. I know that 0 or 1 will take one bit.
But when I type realmax('double') I get a really big number 1.7977e+308. How can this number be saved in only 64 bits?
Would appreciate any clarafication. Thanks.
This is not a MATLAB question. A 64-bit IEEE 754 double-precision binary floating-point format is represented in this format:
bit layout:
| 0 | 1 | 2 | ... | 11 | 12 | 13 | 14 | ... | 63 |
| sign | exponent(E) (11 bit) | fraction (52 bit) |
The first bit is the sign:
0 => +
1 => -
The next 11 bits are used for the representation of the exponent. So we can have integers all the way to +2^10-1 = 1023. Wait... that does not sound good! To represent large numbers, the so-called biased form is used in which the value is represented as:
2^(E-1023)
where E is what the exponent represents. Say, The exponent bits are like these examples:
Bit representation of the exponent:
Bit no: | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
Example 1: | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
Example 2: | 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
Example 3: | 0 | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 |
Example 4: | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 0 |
Example 5: | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Base 10 representation:
Example 1 => E1: 1
Example 2 => E2: 32
Example 3 => E3: 515
Example 4 => E4: 2046
Example 5 => E4: Infinity or NaN (**** Special case ****)
Biased form:
Example 1 => 2^(E1-1023) = 2^-1022 <= The smallest possible exponent
Example 2 => 2^(E2-1023) = 2^-991
Example 3 => 2^(E3-1023) = 2^-508
Example 4 => 2^(E4-1023) = 2^+1023 <= The largest possible exponent
Example 5 => 2^(E5-1023) = Infinity or NaN
When E meets 0 < E < 2047 then the number is known as a normalized number represented by:
Number = (-1)^sign * 2^(E-1023) * (1.F)
but if E is 0, then the number if known as a denormalized number represented by:
Number = (-1)^sign * 2^(E-1022) * (0.F)
Now F is basically the what is determined by the fraction bits:
// Sum over i = 12, 13, ..... , 63
F = sum(Bit(i) * 2^(-i))
and Bit(i) refers the ith bit of the number. Examples:
Bit representation of the fraction:
Bit no: | 12 | 13 | 14 | 15 | ... ... ... ... | 62 | 63 |
Example 1: | 0 | 0 | 0 | 0 | 0 ... .... 0 | 0 | 1 |
Example 2: | 1 | 0 | 0 | 0 | 0 ... .... 0 | 0 | 0 |
Example 3: | 1 | 1 | 1 | 1 | 1 ... .... 1 | 1 | 1 |
F value assuming 0 < E < 2047:
Example 1 => 1.F1 = 1 + 2^-52
Example 2 => 1.F2 = 1 + 2^-1
Example 3 => 1.F3 = 1 + 1 - 2^-52
But when I type realmax('double') I get a really big number
1.7977e+308. How can this number be saved in only 64 bits?
realmax('double')'s binary representation is
| sign | exponent(E) (11 bit) | fraction (52 bit) |
0 11111111110 1111111111111111111111111111111111111111111111111111
Which is
+2^1023 x (1 + (1-2^-52)) = 1.79769313486232e+308
I took some definitions and examples from this Wikipedia page.
Related
Conditionally lag value over multiple rows
I am trying to find cases where one type of error causes multiple sequential instances of a second type of error on a vehicle. For example, if there are two vehicles, 'a' and 'b', and vehicle a has an error of type 1 ('error_1') on day 0, it can cause errors of type 2 ('error_2') on days 1, 2, 3, and 4. I want to create a variable named cascading_error that shows every consecutive error_2 following an error_1. Note that in the case of vehicle b, it is possible to have an error_2 without a preceding error_1, in which case the value for cascading_error should be 0. Here's what I've tried: vals = [('a',0,1,0),('a',1,0,1),('a',2,0,1),('a',3,0,1),('b',0,0,0),('b',1,0,0),('b',2,0,1), ('b',3,0,1)] df = spark.createDataFrame(vals, ['vehicle','day','error_1','error_2']) w = Window.partitionBy('vehicle').orderBy('day') df = df.withColumn('cascading_error', F.lag(df.error_1).over(w) * df.error_2) df = df.withColumn('cascading_error', F.when((F.lag(df.cascading_error).over(w)==1) & (df.error_2==1), F.lit(1)).otherwise(df.cascading_error)) df.show() This is my result | vehicle | day | error_1 | error_2 | cascading_error | | ------- | --- | ------- | ------- | --------------- | | a | 0 | 1 | 0 | null | | a | 1 | 0 | 1 | 1 | | a | 2 | 0 | 1 | 1 | | a | 3 | 0 | 1 | 0 | | a | 4 | 0 | 1 | 0 | | b | 0 | 0 | 0 | null | | b | 1 | 0 | 0 | 0 | | b | 2 | 0 | 1 | 0 | | b | 3 | 0 | 1 | 0 | The code is generating the correct cascading_error value on days 1 and 2 for vehicle a, but not on days 3 and 4, which should also be 1. It seems that the logic of combining cascading_error with error_2 to update cascading_error only works for a single row, not sequential ones.
Date have no data and will show the date with Zero Data in Crystal Reports
I have an output like this on my Crystal Reports: Example: Date(as group)| Hours | Counts ---------------------------------- 01-Feb-2018 | 20 | 5 03-Feb-2018 | 25 | 3 04-Feb-2018 | 22 | 3 05-Feb-2018 | 21 | 2 07-Feb-2018 | 28 | 1 10-Feb-2018 | 23 | 1 If you can see, there are days that missing because no data/empty, but I want the missing days to be shown and have a value of zero: Date | Hours | Counts ---------------------------------- 01-Feb-2018 | 20 | 5 02-Feb-2018 | 0 | 0 03-Feb-2018 | 25 | 3 04-Feb-2018 | 22 | 3 05-Feb-2018 | 21 | 2 06-Feb-2018 | 0 | 0 07-Feb-2018 | 28 | 1 08-Feb-2018 | 0 | 0 09-Feb-2018 | 0 | 0 10-Feb-2018 | 23 | 1 Thank you in advanced.
Is AB the equivalent of A^B?
Doing some boolean algebra. I am under the assumption that AB(B+~C) can also be written as A^B ^ (B+~C) OR (A AND B)AND(B OR ~C) Is this true? If that is the case then for the following expression I simplified that if A=1 B=1 C=0 A^B(B^~C) = (1^1) ^ (1 OR 1) = 1 ^ 1 = 1 = True` Does this make sense or does AB mean something else entirely?
Yes, this is correct. AB and A∧B are equivalent. And A | B | C | A∧B | B∨¬C | (A∧B)∧(B∨¬C) ------------------------------------- 0 | 0 | 0 | 0 | 1 | 0 0 | 0 | 1 | 0 | 0 | 0 0 | 1 | 0 | 0 | 1 | 0 0 | 1 | 1 | 0 | 1 | 0 1 | 0 | 0 | 0 | 1 | 0 1 | 0 | 1 | 0 | 0 | 0 1 | 1 | 0 | 1 | 1 | 1 1 | 1 | 1 | 1 | 1 | 1
Which Variables go on which side of a Karnaugh Map
For a Karnaugh map of three or more variables deciding which side the variables go makes the solution easier to spot and simpler. But how do you know which side which variables go on. eg. For variables x, y and z; You could have x and y as column headers and z as a row header or you could have y and z as column headers and x as a row header which would give two different tables
For maps with up to four variables, it is a matter of taste, which variable is put at which side. However, Mahoney maps as extension of Karnaugh maps for five and more variables do require a certain ordering along the side. Expression for the following examples: abcd!e + abc!de Five-input Mahoney map: Equivalent Karnaugh map: de de 00 01 11 10 00 01 11 10 abc +---+---+---+---+ abc +---+---+---+---+ 000 | 0 | 0 | 0 | 0 | 001 | 0 | 0 | 0 | 0 | +---+---+---+---+ +---+---+---+---+ 010 | 0 | 0 | 0 | 0 | 011 | 0 | 0 | 0 | 0 | +---+---+---+---+ +---+---+---+---+ 110 | 0 | 0 | 0 | 0 | 111 | 0 | 1 | 0 | 1 | +---+---+---+---+ +---+---+---+---+ 100 | 0 | 0 | 0 | 0 | 101 | 0 | 0 | 0 | 0 | +---+---+---+---+ +---+---+---+---+ It is always possible to swap variables as shown here: de de 00 01 11 10 00 01 11 10 abc +---+---+---+---+ abc +---+---+---+---+ 000 | 0 | 0 | 0 | 0 | 001 | 0 | 0 | 0 | 0 | +---+---+---+---+ +---+---+---+---+ 010 | 0 | 0 | 0 | 0 | 011 | 0 | 0 | 0 | 0 | +---+---+---+---+ +---+---+---+---+ 110 | 0 | 0 | 0 | 0 | 111 | 0 | 1 | 0 | 1 | +---+---+---+---+ +---+---+---+---+ 100 | 0 | 0 | 0 | 0 | 101 | 0 | 0 | 0 | 0 | +---+---+---+---+ +---+---+---+---+ Here you can find a nice online-tool to draw and simplify Karnaugh-Veitch/Mahoney maps.
pyspark piplineRDD fit to Dataframe column
Before everything i'm new guy in python and spark world. I have homework from university but i stuck in one place. I make clusterization from my data and now i have my clusters in PipelinedRDD aftre this: cluster = featurizedScaledRDD.map(lambda r: kmeansModelMllib.predict(r)) cluster = [2,1,2,0,0,0,1,2] now now i have cluster and my dataframe dataDf i need fit my cluster like a new column to dataDf i Have: i Need: +---+---+---+ +---+---+---+-------+ | x | y | z | | x | y | z |cluster| +---+---+---+ +---+---+---+-------+ | 0 | 1 | 1 | | 0 | 1 | 1 | 2 | | 0 | 0 | 1 | | 0 | 0 | 1 | 1 | | 0 | 8 | 0 | | 0 | 8 | 0 | 2 | | 0 | 8 | 0 | | 0 | 8 | 0 | 0 | | 0 | 1 | 0 | | 0 | 1 | 0 | 0 | +---+---+---+ +---+---+---+-------+
You can add index using zipWithIndex, join, and convert back to df. swp = lambda x: (x[1], x[0]) cluster.zipWithIndex().map(swp).join(dataDf.rdd.zipWithIndex().map(swp)) \ .values().toDF(["cluster", "point"]) In some cases it should be possible to use zip: cluster.zip(dataDf.rdd).toDF(["cluster", "point"]) You can follow with .select("cluster", "point.*") to flatten the output.