This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
I'm new to clojure and learning basic built in functions. I noticed some weird error when summing values.
user=> (+ 0.99 10 10)
20.990000000000002
user=> (+ 0.99 30 10)
40.989999999999995
However, (+ 0.99 10 30) gives the expected result, 40.99. What do you think is causing this? I'm just using the terminal window on Mac and clj command for the clojure repl. Thank you.
This appears to be the same question as posted on Reddit (and answered there): https://www.reddit.com/r/Clojure/comments/nia8lo/weird_partial_error/
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have working code where I get hash code of data structures with (hash data-structure) but it works in small amount of them. When I have few millions of data structures it will start giving me duplicate hashes (clash). How to get something more unique when structure of data structure is unknown?
You could always resort to md5 (or anything else, really).
(defn md5 [^String s]
(let [algorithm (MessageDigest/getInstance "MD5")
raw (.digest algorithm (.getBytes s))]
(format "%032x" (BigInteger. 1 raw))))
(defn md5-hash [s] (md5 (pr-str s)))
(md5-hash {:a [1 2 3 '(5 6 7)]})
=> "8f941424e629b876cdcb51509521870d"
also, you can use Java serialization instead of pr-str.
This library has existed for a while, and I've seen at least one alternative.
This question already has answers here:
How to code a modulo (%) operator in C/C++/Obj-C that handles negative numbers
(16 answers)
Closed 6 years ago.
This is hopefully a really simple issue that I am overlooking. I am writing in C++ on Qt and I am working on ciphers. I have to use the %26 in order to get them working. I tested this: qDebug() << (6-18)%26; and according to the internet and math, it should produce me with the number 14, right? No. I get -12 which is what 6-18 is, so I don't know if the modulo isn't being applied of what. Any help is greatly appreciated.
You are right on this:
6-18 = -12
When I divide:
-12/26 = 0.46
With modulus = 4
I Hope this help you.
This question already has answers here:
How to measure the inner kernel time in NVIDIA CUDA?
(2 answers)
Closed 8 years ago.
I searched a bit but all things I found could only be annotated in CPU code, how could I measure partial time inside kernel between 2 _syncthread() of 1 threadblock? Is it possible?
One approach is to use the clock() or clock64 function as described in the programming guide.
Search the cuda tag on clock64 for additional examples of its usage.
This question already has answers here:
Optimizations for pow() with const non-integer exponent?
(10 answers)
Closed 9 years ago.
I have to raise a number to the power of 1/2.2 which is 0.45454545... many times. Actually I have to do this in a loop. Simple pow/powf is very slow (when I comment out this pow from my loop code it's a loot faster). Is there any way to optimize such operation?
You might give a look at: Optimized Approximative pow() in C / C++
It also includes a benchmark.
This question already has answers here:
Generating a random integer from a range
(14 answers)
Closed 9 years ago.
I'm having a problem with my code, in which I try to generate a number between 0 and a dim user-defined variable. The line I'm having problem is:
arrayPos = rand()%dim;
I already called srand(time(NULL)) and the arrayPos is getting some wierd numbers like 9.267e-315 and so on.
Any ideas on how to fix it?
Thank you
The problem really was garbage memory, I restarted the computer and it worked like a charm