strtod change last digit - strtod

someone can explain to me what is happening?
double dbl = stdtod("9999999999999999", NULL);
I got the value: 9999999999999998
and not: 9999999999999999
I don't understand why this happen, in the MSDN, is specified that the max double number is 1.7976931348623158E+308, so it can't be a overflow.
Thanks

Its not an overflow its an issue with how floating point works. You have a limited nubmer of significant digits that is exceeded long before you get an overflow. This has been linked hear many times but check out this

Related

App Inventor app does not take answers in divisions

I am trying to make an app that has the four basic mathematical operations. Addition, subtraction, multiplication and division.
Each operation has a series of exercises and for each correct exercise, assign a point in a score counter.
I clarify that both, the exercises and the answers are selected at random. They are not questions and answers stored in a list.
Everything is ready and finished, but I have a problem with the division and it is as follows.
If, for example, the result of the division has exactly two decimals, the score counter takes as correct the answer that is selected. But if the result of the division has more than two decimals, the score counter does not take the answer as correct.
Example:
20/8 = 1.25 No more decimals, then the score counter takes it as the correct answer
9/7 = 1.28571428571 This answer has many decimals, then the score counter does not take it as the correct answer
The problem is not in rounding up the figures or in formatting the number of decimals. The problem is that for some reason, answers with more than two decimal numbers are not taken as correct.
Not matter if I round the result to a integer or if I set only a 2 decimals for each result, for some reason, the score counter do not show the result as correct.
For example, if I take the division 9/7 = 1.28571428571 and I set only 2 decimals for the result, leaving it as 1.28, the score counter do not take this result as a correct result.
Even if I round the result to 1, occur the same problem.
How can this be fixed?
Many thanks to anyone who can help me find a solution.
P.S.: I'm not a programmer I'm just an amateur and nothing else, that just starts, so I appreciate, please, answers for a layman like me. Thanks in advance.
Here are the blocks

How is it possible to have 0 bits per character?

I have an example of a string ABABABAB and I have to calculate an entropy of this string.
It's obvious that i can get different numbers when taking different alphabets. When I took alphabet A={a,b} I got an answer for entropy = 1 bit per character(Using Shannon's formula) => means 8 bits for a whole string.
But what about a case when we take A={ab,aa,bb,ba}?
We get entropy =0 bits per character (which is also obvious,as there is no randomness). How is it possible to have 0 bpc ? So the whole string = 0 bits?=/ I can't understand where I got wrong..
Thanks in advance for any kind of help.
Yes, it's possible, but other information needs to be sent. In particular that there are four encoded symbols, and that the only possible symbol is AB. Once you've sent those things, the remainder is zero bits.

C comparing times that can overflow

I need to detect the overflowing of an unsigned long.
This variable holds the amount of milliseconds since the device is running (it's an Arduino). Doing sizeof(unsigned long), I have come to see it's indeed a 32-bit number. Now, since it increments every millisecond, which means the device will run for about 49 days before this value overflows.
Since it's for a home system, it isn't really advisable. Now what I'm using the number for, is comparing if the current time is larger than the previous time plus an amount of debouncing.
if(timeChanged + amountOfMs < currentTime){ ... }
Needless to say, once overflow occurs this isn't going to work anymore. What's an efficient way to solve this? I've thought about also having a second-timer as well to check if the milliseconds one has overflowed, but in the end I'll have the same problem.
This rollover issue seems to cause quite a bit of confusion...
The right answer is that you need not worry about the millis() rollover, as long as you do your calculation properly.
This is bad:
if (timeChanged + amountOfMs < currentTime) { ... }
This is good (rollover-safe):
if (currentTime - timeChanged > amountOfMs) { ... }
The reason it works is that arithmetics with unsigned integers (unsigned long in your case) reliably works modulo max+1 (ULONG_MAX+1 is 232). Thus, currentTime, timeChanged and their difference always have the correct value, modulo 232. As long as you test your button more often than once every 49 days (which is likely) the difference will be in the range of an unsigned long, and your test will be correct.
Let put it another way: if millis() rolls over between timeChanged and currentTime, then the difference currentTime - timeChanged will be negative. But since the difference is actually computed with unsigned numbers, it will underflow and roll-over to the correct result. I do not like this explanation though, as it sounds like an error compensating another error. The truth is: if you think of unsigned numbers in terms of modular arithmetics, there is no error anywhere.
This is such a common mistake (and one that I've made myself) that the Arduino Playground has a nice, thorough, and correct answer. See https://playground.arduino.cc/Code/TimingRollover
You can create a new if loop checking the condition : if(currentTime == 0xFFFFFFFE)
If this is the condition, next millisecond will overflow your variable. So at this point you can manually reset it to zero and goto loop where it starts from zero.
This might or might not help your situation. I can't say for sure because you haven't shared any further details about your code.
Define two variables, I'm going to call them 'now' and 'lastNow'.
unsigned long now;
unsigned long lastNow = 0;
In your loop you can now do this:
now = millis();
if (now < lastNow) {
// rollover!
}
lastNow = now;
Nice and reliable regardless of how frequently (or infrequently) you loop.

How to write an assembly sorting program 8086 with works with 6 digits numbers? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
im new to assembly language and i know many codes.
However im working with 8086 emulator with only works with 16 bit numbers.
this is a home work that im really stuck in :
How can i write an assembly code with do the following :
1-get 20 , maximum 6-digits decimal numbers and store them in an array.
2- sort the array in ascending order.
its really hard for me to understand how to manage registers and stack for this long numbers.
every help will be appreciated in advance .
In order to sort 32bit numbers (or broader) with 16bit registers you have to compare the upper part of each number separately.
Assume we have these random two 32 bit numbers (shown in hex) 4567afdf and 321abc09.
Now when you look at them as 16 bit values they look like this:
4567 afdf
321a bc09
As you can easily see, the upper 16 bits you can compare individually.
If the upper 16 bits are higher or lower, then you know that the lower part doesn't matter anymore and you sort them accordingly.
If the upper 16 bits are equal, then you compare the lower 16 bits and if they are also equal, both numbers are equal => no sort needed, otherwise you shuffle them accordingly. Since the upper 16 bits are also equal, you don't even need to shuffle them.
If the upper 16bits are different, you still have to shuffle the lower 16 bits accordingly, as they might be different.
The basics of this approach can be used for an arbitrary number of bits not just 32bits. Generally when you have a seemingly hard problem, you should try to think of the easy examples and how you can solve it. Then you can extend it to more complicated cases.
EDIT:
An alternative approach would be, if you have strings of decimal numbers and you want to sort them based on the string representation instead of the numbers.
In this case, you can do it as follows
If the length of the two number strings are differnt, the shorter one is the lower number.
if the length is equal, then you can look at each digit individually (starting with the first digit) until you hit a non-equal digit or the string end. If you reaced the end of the string, the numbers are the same, otherwise you kn ow which one is higher/lower.

VB6: what is likely cause of Overflow error when using strings?

In VB6, what is the likely cause of an Overflow error? I am using strings when it occurs.
Is it RAM, or hard-drive space? Or is it something internal to VB6?
I am going to take a stab in the dark here, some code would help as Hogan said. Typically overflow error occur with string when VB6 things it is dealing with integer or longs in math formulas and the results are too large for a integer or long to hold.
Depending on the nature of your formula you may get away from the problem by forcing the system to use the numbers as floating point by adding a '.0" at the end. Otherwise use the various Cxxx function to explicitly convert the numbers to a type that has a greater range.
The one thing you consider is that floating point are less precise than whole number integers so make sure you don't lose needed precision when you do the conversion.
Another 'stab', since no code is provided ...
The following will produce an overflow error:
Dim x as integer
x = len(longstring) 'longstring over 32,768 characters in length
Would cause an overflow error.
Dim x as long
x = len(longstring) 'longstring over 32,768 characters in length
Works fine.
Another example of an overflow from Microsoft Support here:
EDIT
A more subtle situation that can catch you off guard:
You attempt to use a number in a calculation, and that number is coerced into an integer, but the result is larger than an integer.
Dim x As Long
x = 2000 * 365 ' Error: Overflow
To work around this situation, type the number, like this:
Dim x As Long
x = CLng(2000) * 365

Resources