The Big Programming Thread - Page 146
Forum Index > General Forum |
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. | ||
green.at
Austria1459 Posts
| ||
ranshaked
United States870 Posts
On June 22 2012 07:00 Abductedonut wrote: I whipped up some quick code for you. It's by no means pretty. Here you go. Assuming you're using C, same principles should apply in C++. Next time, specify which language you use! + Show Spoiler +
The basic idea between comparing floats is that you must have a general "error" range that you're okay with. Most floats, when printed, will get rounded off. So it may seem like the answer is 1.3333334 but in memory its actually stored as 1.333333333333333333333. So when you do the comparison, it gets jacked up. Read the article I commented in the code. Also, does anyone have expierence with Microsoft's Media Foundation? I've been trying to use MF to capture to a hwnd using D3D and I'm having a really hard time. Would appreciate any help. Thank you very much, it definitely helped. Would also happen to know why when I type in an answer to a division problem exactly the way I printf the answer, that it still doesn't read it in as correct? Basically this is what is happening: I'll post an image. I have it set to debug in which it gives me the answer, then I type in the answer and it should say "correct!", but for some reason, I type in EXACTLY what it prints out the answer to be, and it's still reading it as wrong. Here's an image. ![]() Thanks. I've tried using %.2f to limit it, %f, %lf and it's always random, some division problems will read my answers properly, and some won't. I marked the two that are not are opposite Edit: I'm reading that you cannot directly compare floats :/ unless you use a function, but unfortunately I was never taught this! Edit again: This happens with both your code, and my original "if" statements. The problem is this: If it's not division, the answer has to be an integer, but if it's division it has to be two decimal places, like 2.12... When you're casting floats or doubles with (double) x / ((double)y) can you cast that directly to two decimal places? Like doing (double.2)x ? | ||
Abductedonut
United States324 Posts
On June 24 2012 12:03 ranshaked wrote: Thank you very much, it definitely helped. Would also happen to know why when I type in an answer to a division problem exactly the way I printf the answer, that it still doesn't read it in as correct? Basically this is what is happening: I'll post an image. I have it set to debug in which it gives me the answer, then I type in the answer and it should say "correct!", but for some reason, I type in EXACTLY what it prints out the answer to be, and it's still reading it as wrong. Here's an image. Thanks. I've tried using %.2f to limit it, %f, %lf and it's always random, some division problems will read my answers properly, and some won't. I marked the two that are not are opposite Edit: I'm reading that you cannot directly compare floats :/ unless you use a function, but unfortunately I was never taught this! Edit again: This happens with both your code, and my original "if" statements. The problem is this: If it's not division, the answer has to be an integer, but if it's division it has to be two decimal places, like 2.12... When you're casting floats or doubles with (double) x / ((double)y) can you cast that directly to two decimal places? Like doing (double.2)x ? You're thinking of it the wrong way. How you PRINT decimals to the screen and how the computer actually stores them are two completely different things. When you use printf, cout, whatever output you use, those functions are written to take the value inside the computer and print it in a way you specify. The printf family of functions doesn't particularly care if the value inside the computer is 2.999999999 or 3.0000000001. It will print out 3 if specify it to one digit. Now when you do a comparison inside your program, such as: if ( float1 == float2 ) Then the computer assumes that they must be the same at EVERY SINGLE DECIMAL SPACE PAST THE DECIMAL POINT. So as a concrete example, if float1 = 1.9999999999999999 and float2 = 1.9999999999999998 then as far as the computer is concerned, they are not equal. Whoops, your above if statement would fail. That is why floats need an acceptable margin for error. To you, float1 and float2 are pretty much the same. I mean, who cares about the 10th or 11th decimal place not being the same, right? That is why the more LOGICAL equivalent for the comparison above is like this: if ( ABSOLUTEVALUE( float1 - float2 ) <= 0.000000000001 ) This above comparison basically says "okay look, if these floats are the same up till this decimal point, then I pretty much consider them equal." Well guess what? Everytime you call scanf, the value the user puts in, such as 1.24, it can actually get stored as 1.2399999999999999999999999. That is precisely why your program is failing, and that is precisely what that exercise is supposed to teach you. Here's working code, in case you need an even more concrete example: + Show Spoiler + Here are some of the results when you run the code in the spoiler: 43 / 3 Result is: 14.33 What is the answer? 14.33 you entered: 14.3299999237 But trancted to two decimal places.. you entererd: 14.33 Correct! 50 / 4 Result is: 12.50 What is the answer? 12.50 you entered: 12.5000000000 But trancted to two decimal places.. you entererd: 12.50 Correct! 89 / 96 Result is: 0.93 What is the answer? 0.93 you entered: 0.9300000072 But trancted to two decimal places.. you entererd: 0.93 Correct! 38 / 44 Result is: 0.86 What is the answer? 0.86 you entered: 0.8600000143 But trancted to two decimal places.. you entererd: 0.86 Correct! 60 / 8 Result is: 7.50 What is the answer? + Show Spoiler +
| ||
ranshaked
United States870 Posts
On June 24 2012 12:54 Abductedonut wrote: You're thinking of it the wrong way. How you PRINT decimals to the screen and how the computer actually stores them are two completely different things. When you use printf, cout, whatever output you use, those functions are written to take the value inside the computer and print it in a way you specify. The printf family of functions doesn't particularly care if the value inside the computer is 2.999999999 or 3.0000000001. It will print out 3 if specify it to one digit. Now when you do a comparison inside your program, such as: if ( float1 == float2 ) Then the computer assumes that they must be the same at EVERY SINGLE DECIMAL SPACE PAST THE DECIMAL POINT. So as a concrete example, if float1 = 1.9999999999999999 and float2 = 1.9999999999999998 then as far as the computer is concerned, they are not equal. Whoops, your above if statement would fail. That is why floats need an acceptable margin for error. To you, float1 and float2 are pretty much the same. I mean, who cares about the 10th or 11th decimal place not being the same, right? That is why the more LOGICAL equivalent for the comparison above is like this: if ( ABSOLUTEVALUE( float1 - float2 ) <= 0.000000000001 ) This above comparison basically says "okay look, if these floats are the same up till this decimal point, then I pretty much consider them equal." Well guess what? Everytime you call scanf, the value the user puts in, such as 1.24, it can actually get stored as 1.2399999999999999999999999. That is precisely why your program is failing, and that is precisely what that exercise is supposed to teach you. Here's working code, in case you need an even more concrete example: + Show Spoiler + Here are some of the results when you run the code in the spoiler: 43 / 3 Result is: 14.33 What is the answer? 14.33 you entered: 14.3299999237 But trancted to two decimal places.. you entererd: 14.33 Correct! 50 / 4 Result is: 12.50 What is the answer? 12.50 you entered: 12.5000000000 But trancted to two decimal places.. you entererd: 12.50 Correct! 89 / 96 Result is: 0.93 What is the answer? 0.93 you entered: 0.9300000072 But trancted to two decimal places.. you entererd: 0.93 Correct! 38 / 44 Result is: 0.86 What is the answer? 0.86 you entered: 0.8600000143 But trancted to two decimal places.. you entererd: 0.86 Correct! 60 / 8 Result is: 7.50 What is the answer? + Show Spoiler +
Thank you, this is much easier to understand than the things I've been reading. I understand now, I assumed that by using a float it automatically limited the answer to only 6 decimal places, I didn't realize that even though it doesn't show, it's still checking down the line. I will try this and let you know. This stuff is fascinating, and challenging. I dislike how I've been stuck on this all day, but i'll play with it now. I actually enjoy spending hours trying to figure stuff out and playing with it, but sometimes I get stuck like this! | ||
Abductedonut
United States324 Posts
On June 24 2012 13:03 ranshaked wrote: Thank you, this is much easier to understand than the things I've been reading. I understand now, I assumed that by using a float it automatically limited the answer to only 6 decimal places, I didn't realize that even though it doesn't show, it's still checking down the line. I will try this and let you know. This stuff is fascinating, and challenging. I dislike how I've been stuck on this all day, but i'll play with it now. I actually enjoy spending hours trying to figure stuff out and playing with it, but sometimes I get stuck like this! No problem. That's what this thread is for! Here's another really creative way you could have solved this problem, btw. I feel like you'd like this.
| ||
Ghad
Norway2551 Posts
![]() http://www.ndcoslo.com/Article/News/2012video | ||
Iwbhs
United States195 Posts
Any idea on why my tooltip breaks when I include html? | ||
berated-
United States1134 Posts
On June 25 2012 09:12 Iwbhs wrote: http://pastebin.com/myhvzy1q Any idea on why my tooltip breaks when I include html? Looks like you need to escape your quotes. onmouseover="doStuff(\"escape me \")" | ||
Iwbhs
United States195 Posts
<div id="shoulder" ONMOUSEOVER="ddrivetip('1 Armor<br /><a href="/class.php?c=Monk&d=Shoulder">Shoulder</a><br /> All Resistance: 1<br />','black')"; ONMOUSEOUT="hideddrivetip()" style="background-image: url(images/Shoulder.png);"></div> where is the mistake in quotes? <div id="shoulder" ONMOUSEOVER="ddrivetip('1 Armor<br /><a href=\"/class.php?c=Monk&d=Shoulder\">Shoulder</a><br /> All Resistance: 1\n','black')"; ONMOUSEOUT="hideddrivetip()" style="background-image: url(images/Shoulder.png);"></div> not working. | ||
berated-
United States1134 Posts
On June 25 2012 09:22 Iwbhs wrote: <div id="shoulder" ONMOUSEOVER="ddrivetip('1 Armor<br /><a href="/class.php?c=Monk&d=Shoulder">Shoulder</a><br /> All Resistance: 1<br />','black')"; ONMOUSEOUT="hideddrivetip()" style="background-image: url(images/Shoulder.png);"></div> where is the mistake in quotes? <div id="shoulder" ONMOUSEOVER="ddrivetip('1 Armor<br /><a href=\"/class.php?c=Monk&d=Shoulder\">Shoulder</a><br /> All Resistance: 1\n','black')"; ONMOUSEOUT="hideddrivetip()" style="background-image: url(images/Shoulder.png);"></div> not working. yeah mental lapse, you can't use \ in html, you would have to escape it using html ". The best way to avoid this is probably restructure your code a little bit, but thats a different argument. To get it working pick one quote for the outside and one for the inside, what I mean by that is.. <div id="shoulder" ONMOUSEOVER='ddrivetip("1 Armor<br /><a href=\"/class.php?c=Monk&d=Shoulder\">Shoulder</a><br /> All Resistance: 1\n","black");' ONMOUSEOUT="hideddrivetip()" style="background-image: url(images/Shoulder.png);"></div> You also have to escape the quote for the js Edit: Yeah okay this is painful a couple questions... 1) Is there any reason you aren't using jquery? 2) Even if you aren't using jquery you can still accomplish this. It would probably be cleaner if you hid the html that you wanted to use in a div somewhere on the page and then grabbed it load the tip. Example: ( I didn't test this, just trying to show the general concept ) <div id="shoulder" ONMOUSEOVER="ddrivetip('tooltiptext','black');" ONMOUSEOUT="hideddrivetip()" style="background-image: url(images/Shoulder.png);"></div>
| ||
3FFA
United States3931 Posts
![]() | ||
![]()
tofucake
Hyrule18980 Posts
| ||
3FFA
United States3931 Posts
I've already been told how things like a 1 instead of an i can mess up coding so much that you may spend hours asking people to help you look through it, possibly even a whole day. | ||
Blisse
Canada3710 Posts
-_____-" | ||
Azerbaijan
United States660 Posts
| ||
jergason
United States37 Posts
http://www.amazon.com/C-Programming-Language-2nd-Edition/dp/0131103628/ref=sr_1_1?ie=UTF8&qid=1340600201&sr=8-1&keywords=the c programming language | ||
supereddie
Netherlands151 Posts
On June 25 2012 09:45 berated- wrote: 2) Even if you aren't using jquery you can still accomplish this. It would probably be cleaner if you hid the html that you wanted to use in a div somewhere on the page and then grabbed it load the tip. Example: ( I didn't test this, just trying to show the general concept ) <div id="shoulder" ONMOUSEOVER="ddrivetip('tooltiptext','black');" ONMOUSEOUT="hideddrivetip()" style="background-image: url(images/Shoulder.png);"></div>
Even easier is just use CSS.
Ofcourse, this won't work in Internet Explorer < 8, but it works in any recent browser so who cares? | ||
Poltergeist-
Sweden336 Posts
| ||
Joefish
Germany314 Posts
On June 25 2012 19:10 Poltergeist- wrote: What are the latest and greatest/upcoming programming languages, if any? Anyone have any idea? Does it differ possibly between the US and EU? According to this website, the top 3 are C/Java/C++. And I don' think there is any difference between different regions. But haven't found any data about it. I'm actually surprised C being at the top of the charts. Thought top 3 would look like this C++/Java/PHP. | ||
Isualin
Turkey1903 Posts
| ||
| ||