postgres double precision [closed] - postgresql

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
select round(product_qty * 100) - product_qty as test,
id, product_qty
from stock_move
where product_id=63
and state='done'
and id=45058;
test | id | product_qty
54.45 | 45058 | 0.55
(1 ligne)
select round(product_qty * 100) - (product_qty*100) as test,
id,
product_qty
from stock_move
where product_id=63
and state='done'
and id=45058;
test | id | product_qty
-7.105427357601e-15 | 45058 | 0.55
(1 ligne)
can somebody explain me these results?

Floating-point representations like double precision fields in pg are by their very nature subject to rounding errors. This paper gives a good mathematical introduction.
How you deal with these rounding errors depends on a bit on your application, and varies between:
just living with them eg by sort of ignoring them - format that result as %.2f and the error will become invisible. Just make sure you understand all the consequences of that choice!!
switching datatypes to something that by design is not subject to rounding issues - check out pg's money type and the exact numeric types.
The latter should be the preferred approach especially in contexts where your application handles money.

You're getting those results because product_qty*100 has different IEEE-754 binary representation than round(product_qty * 100):
without round : 100000001001011100000000000000000000000000000000000000000000001
with round : 100000001001011100000000000000000000000000000000000000000000000
It's easy to see that 55 should be represented as 1,71875 * 25 (as in with round), but due to floating point arithmetic product_qty*100 is slightly different.
If you want to still live with floating point number, then I recommend to read "What every computer scientist should know about floating-point arithmetic" paper, or just switch to numeric type with arbitrary precision.

Related

How long would it take my i-7 processor to factorise a 1024 bits number (consisting of just 2 prime factors) [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
We're examining the RSA algorithm and would like to know how much time it would take an intel i-7 core (# 2.50 gHz) to factorise the RSA-public key.
we wrote a piece of java for this, and I don't know how effective it is
public static String factorise(long l)
{
double a = Math.floor(Math.sqrt(l));
while(l/a != Math.round(l/a))
{
a--;
}
return (long)a + ", " + (long)(l/a);
}
With a number around 2^45 it took the PC approximately 33 milliseconds. In theory, how long would it take to factorise a number around 2^1024?
Thanks in advance :)
Your algorithm is O(2^n), where n is the number of bits in the original number l. (that means that a single bit more will double the runtime, because twice as many numbers a must be checked - on average)
If 45 bits took 33 ms, then 1024 bits will take approx. 2^1024 / 2^45 * 33ms = 5.34654 * 10^285 years.
This of course assumes, that the 1024bit code is exactly as efficient as your code for long numbers (64bit?). Which is a bold statement, considering that 10^285 years is more than enough time to switch to the General number field sieve and scratch a few million years of that time...
In 2009 the 768 bit number rsa-768 was cracked using about 1000 cores and 2 years of calculations. Assuming they used the General number field sieve (a very fair assumption) it would take them 7481 years to crack a 1024 bit number using the same hardware.
Or using only your i7 with this algorithm: about 3 million years. Still a long time.... ;)

coin flip generator [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
what do you guys think will be the outcome if a program is coded to simulate a coin flip where there is a 50% of the coin landing either heads or tails when the results are looked at; Mainly will there be a higher % of the coin flips landing heads when the previous 10 flips were tails and vice versa?
This really depends on what mechanism is being used to generate the random numbers. If, say, a linear congruential generator is used...
... then clearly any given generated number is dependent on the one that preceded. The quality of the output also depends on what parameters are used in conjunction with the mechanism (e.g. if a small value was used for "m" in the above method, the quality would be poor... or if your seed value was highly predictable).
Despite the fact that computers only generate pseudo-random numbers, some algorithms satisfy the tests for statistical randomness (i.e. have no discernible patterns) and can be used safely.
If you are that concerned about the randomness of your generated numbers, you should look into the actual method being used to generate them within your specific context. For more information, take a look at Wikipedia.
If you program it correctly, the chance of landing on either side of the coin should be equal(50%), regardless of previous flips...
The probability of heads/tails is always 50% on any given toss. The probability of getting x heads (or any specified combination of heads/tails) in a row is 0.5^x (because each toss is independent of the others).
I'm I am understanding you correctly, you are asking if there will be more "heads" results or "tails" results if a program is written to give each option a 50% chance?
Statistically, if you run the program a number of times, each side will average out and you'll have an equal number of heads and tails results. (Depending on your language of choice, you may have to seed the randomizer to guarantee true randomness.)
I suppose that it depends on the goodness of the pseudo-random number generator.
On only ten flips, the results may be meaningless...
But if you have a good algorithm to generate pseudo-random number, and you extend this experiment with n try where "n" is a significantly large large, the probability still remain 0.5 (50%).
This because the statistic has no memory

How to calculate a number like PI over multiple machines? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
In what way would you try to use multiple computers to compute a number like PI, i.e.?
Are there existing algorithms or solutions that make this easy to do?
How do you split up the work and let the results from other machines come into effect?
Here's one simple way:
generate a huge number of random (x,y) points where x and y are between 0 and 1.
for each point, calculate whether its cartesian distance to the origin is <= 1 (that is, whether it lies on or inside the circle)
count the number of point inside the circle versus outside the circle
Pi, then, can be calculated from the ratio of inside to outside points. A very large number of points is necessary for this to approach pi, but if you have many machines, you can have each computer generate as many as you like, then simply return the counts to some leader machine, which would collect all the results and calculate the final ratio.
This method can be used to calculate pi to any precision you want...the more points, the more precision. It's called a 'Monte Carlo' method because it uses randomness. See http://math.fullerton.edu/mathews/n2003/montecarlopimod.html for more information.
An "easy" version would be using the Bailey–Borwein–Plouffe formula, or its faster variant Bellard Formula. It allows calculating individual (binary) digits of π without calculating the previous ones before.
This means that you can distribute your calculation effort on different computers, which do not have to communicate much. For larger digit indices, you still need distribute the calculation even for a single digit (since you are doing some multiplications and divisions of really large integers).
This was used by the PiHex project to calculate some (binary) digits around digit number 5·1012, some around 4·1013 and some around 1015.
On .Net platform, you can try .net remoting

Calculate the numbers in Scala [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I can calculate the expressions, and term but I don't understand what is tregonometric functions. Are they Expressions or Terms or Items? How to write the algorithm of calculation of Pi and tregonometric functions. Cos, sin....
Please if you know give me a hint.
Try a web search again, this time using the correct spelling "trigonometric functions". Algorithms for calculations like this date back at least to ancient Greece and you'll find them in a great many maths textbooks.
I suggest you try to do your own homework, then (and only then) come back to stack overflow if you have some particular issue with Scala syntax that prevents you from implementing your chosen algorithm. In the meantime, this isn't a proper or valid question.

Calculate average rating [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
Can any one tell me how to calculate rating average?
I interpret rating average as the average of a set of ratings.
In that case you simply compute
sum_of_all_ratings
rating_average = ------------------
number_of_ratings
I recommend this
example :
Ordinary average : (a+b+c....z)/(number_added_items)
Rating average : (va*a + vb*b + vc*c... +vz*z)/(number_added_items)
In the first case you could say that the weight for each item is 1 as all items have the same weight in the calculation, while in the second case weights differ)
In your case the weight for each rating vote might depend on the voter so let's say a highly rated voter has a ponder for 3 his vote while a novice has 1.If 3 novices vote for an answer the rating of that answer will be (1*1+1*1+1*1)/(3) = 1 while if 2 high voters and a novice vote for an answer the average will be 3*1+3*1+1*1/3 = 2.33.
Does this answer your question? otherwise please be more explicit.