|
I'm working C in Gedit for an edX class and have almost finished this assignment but I keep getting expected expression errors while compiling. Here is the codecode:
code:
It also is giving me the wrong answer for .41, and is saying 5 coins instead of 4. All the other test answers are going okay except 0.41. Any hints on where I might be going wrong (without anyone blatantly giving me the code) will also be appreciated. This is the code that compiles okay and is mostly correct: code:
durtan fucked around with this message at 23:37 on Feb 21, 2014 |
# ? Feb 21, 2014 22:48 |
|
|
# ? Jun 1, 2024 23:11 |
|
When you write C++ code:
C++ code:
C++ code:
|
# ? Feb 21, 2014 23:20 |
First, you should use [code] tags instead of [quote] tags to post program code, it formats much nicer, and shows up when you press the Quote button on the forums. Second, your problem is that you aren't grouping your statements into blocks. Add some { } around the stuff your individual if-conditions applies to.
|
|
# ? Feb 21, 2014 23:22 |
|
nielsm posted:First, you should use [code] tags instead of [quote] tags to post program code, it formats much nicer, and shows up when you press the Quote button on the forums. Thanks for the [code] info. I fixed my original post. And yes, adding {} definitely helped the expected expression issues. Thanks for the advice Xerophyte and nielsm. In regards to getting the wrong output, I'm fairly certain it has something to do with how I'm rounding the float. I will mess around a bit with it.
|
# ? Feb 21, 2014 23:39 |
|
You should consider using an integer to store the number of pennies instead of floating point.
|
# ? Feb 21, 2014 23:49 |
|
taqueso posted:You should consider using an integer to store the number of pennies instead of floating point. Do you mean the money variable being stored as an integer? Doesn't the underlined part convert the float to an int? Or is it converting it back to a float because I declared the variable a float in the beginning? quote:money = (int) round(money*100); Edit: I apologize if I'm asking stupid questions, this is new stuff to me and I'm trying to research the best I can. durtan fucked around with this message at 00:13 on Feb 22, 2014 |
# ? Feb 22, 2014 00:05 |
A variable stays the type it was declared as. When you assign something to it, it either gets converted in one way or another (depending on the types involved), or there is no automatic conversion and the assignment is a compile-time error. The effect of that int cast is really just performing a truncation operation on the number, it gets converted to int and right back to double.
|
|
# ? Feb 22, 2014 00:16 |
|
So would it make sense to create an int variable from the double variable? Edit: Doesn't seem to make a difference. Creating an int variable from the float variable gives me the right number but I'm still counting 5 coins instead of 4. Adding a <= 0 check at the beginning of the for loop and getting the coins variable to increment in each else if statement doesn't seem to give me the right answer either. code:
durtan fucked around with this message at 00:47 on Feb 22, 2014 |
# ? Feb 22, 2014 00:19 |
|
durtan posted:So would it make sense to create an int variable from the double variable? I think what he's trying to get at is floats don't necessarily behave exactly as you think they will. Generally, it's considered a best practice to store monetary information in fixed point form because $CASHMONIES * $SOME_VAL / $SOME_VAL may not return the desired result. Machine epsilon, etc.
|
# ? Feb 22, 2014 01:11 |
|
durtan posted:Still gives me five coins instead of four when I enter ".41". Try making it print what the coins are. And/or trace through in your head or a debugger what path the code will take with that input. (It's a problem with your logic, rather than syntax or floating point rounding)
|
# ? Feb 22, 2014 01:13 |
|
For my architecture class, I have to write a bunch of C functions that will tell if a given double is negative, zero, denormal, NaN, etc. I got started on it and it was going fine, until I was told we can't use floating point operations, so there goes my whole "shift and/or mask, compare to 0" method for figuring stuff out. Now I'm completely lost. Can anyone point me in the right direction?
|
# ? Feb 22, 2014 02:54 |
|
Comparing to zero doesn't have to be a floating point op.
|
# ? Feb 22, 2014 03:04 |
|
Spatial posted:Comparing to zero doesn't have to be a floating point op. Yeah, I misunderstood the professor when he told me that.
|
# ? Feb 22, 2014 03:10 |
|
hooah posted:For my architecture class, I have to write a bunch of C functions that will tell if a given double is negative, zero, denormal, NaN, etc. I got started on it and it was going fine, until I was told we can't use floating point operations, so there goes my whole "shift and/or mask, compare to 0" method for figuring stuff out. Now I'm completely lost. Can anyone point me in the right direction? It sounds like your teacher wants you to learn about the IEEE floating point format. Start with this since doubles are specified http://en.wikipedia.org/wiki/Double_precision_floating-point_format and that, roughly, code:
|
# ? Feb 22, 2014 03:16 |
|
Nb. your compiler may yell at you for doing type punning that way :/
|
# ? Feb 22, 2014 03:51 |
|
delta534 posted:
code:
|
# ? Feb 22, 2014 04:11 |
|
schnarf posted:memcpy() is one safe way to do it
|
# ? Feb 22, 2014 07:40 |
|
unions or memcpy, the compiler is smart enough to recognise either and make it efficient. The classic union required being:C code:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms724284(v=vs.85).aspx
|
# ? Feb 22, 2014 16:26 |
|
I'm working on the is_denormal function, but for some reason the program isn't getting into one of my if blocks. Here's the function:C code:
|
# ? Feb 22, 2014 18:01 |
|
ExcessBLarg! posted:Unions are kosher for type punning in C99. Aren't they? From what I can see on this page, C99 says that union-based type punning is well-defined, but the C++ standard doesn't appear to say anything on the matter.
|
# ? Feb 22, 2014 18:55 |
|
Anyone here ever written Conway's Game of Life? It's my next project for my Foundations of CS class and I have no drat idea what I'm doing.
|
# ? Feb 22, 2014 21:01 |
|
You mean you don't get programming? You don't get the rules of Game of Life? You can't use arrays and translate a simple algorithm into code? What is the nature of your question?
|
# ? Feb 22, 2014 21:36 |
|
Would anyone know an easy way to round a float to one decimal? I was able to fix the logic in my current algorithm but now it is failing a check for 4.2. My print functions are saying the program is converting 4.20000 to 419 and a test program confirms that 4.2 printed to 50 decimals is 4.1999somethingsomething. Here is the code yet again with my test print functions disabled.code:
|
# ? Feb 22, 2014 22:50 |
|
It's not possible to represent 1/10 exactly in floating point format.
|
# ? Feb 22, 2014 23:00 |
|
tractor fanatic posted:It's not possible to represent 1/10 exactly in floating point format. Yup. I was hoping to multiply the 4.1999 by 10 and round it that way before bringing it forward again. I fixed it by rounding the variable by 100 as so: code:
|
# ? Feb 22, 2014 23:42 |
|
You can just add 0.5 and use integer truncation (if it's positive). It wouldn't work if you're one ulp under 0.5 though. Or between 2^23 and 2^24.
|
# ? Feb 22, 2014 23:47 |
|
schnarf posted:From what I can see on this page, C99 says that union-based type punning is well-defined, but the C++ standard doesn't appear to say anything on the matter. You should interpret the C committee's notions of type-based alias analysis as aspirational at best. Seriously, they just can't agree what the actual rule design should be, and it seems like anybody with any mathematical sense fled that committee years ago. It is all really vague, and clang's rule of "if you make it really obvious that you're breaking the type-punning rules, we promise not to gently caress everything up" is probably the best take on the whole thing that anybody's going get.
|
# ? Feb 23, 2014 08:23 |
|
Are there any plans to improve the shittier aspects of C++? I mean god drat. It's 2014 and imports are still done by copy-and-pasting files into each other using preprocessor macros. That is some truly shameful poo poo.
|
# ? Feb 23, 2014 16:51 |
|
Spatial posted:Are there any plans to improve the shittier aspects of C++? I mean god drat. It's 2014 and imports are still done by copy-and-pasting files into each other using preprocessor macros. That is some truly shameful poo poo. There's a group working on modules with tentative inclusion in C++1y, so that's three years off at best? Here's Clang's page on their support for it, and here's the latest paper on the proposal AFAIK.
|
# ? Feb 23, 2014 17:01 |
|
Just learn to love the poo poo pool that is C++ because I doubt it is going to get any better. Honestly people are praising how great C++ 11 has made the language, and I agree in some areas the language is a little better, but it has also made things worse IMO. I'm really hoping rust or D pick up enough steam so I can move on from this poo poo show for good. Until then I'm just going to try and live with the mess. I don't really like how dense the syntax is with rust, and D I am not fond of the garbage collection stuff. It will be interesting to see how these two things evolve. xgalaxy fucked around with this message at 17:59 on Feb 23, 2014 |
# ? Feb 23, 2014 17:57 |
|
What has C++11 made worse?
|
# ? Feb 23, 2014 20:09 |
|
Rottbott posted:What has C++11 made worse? I don't like how rvalue references were implemented. Given the restrictions of not breaking compatibility, etc. there isn't much different they could have done. But I dislike how you have to litter your code with std::move and std::forward, not to mention the horror of T&& in already verbose and hard to read templates. In addition its a easy to screw up rvalue references and end up doing needless copying because you forgot to add an std::move in your copy (move) constructor for example. I think it is a bit of a joke how they introduced this new alternative style function syntax. Maybe instead of introducing a new way of declaring functions you just instead accept the fact that you can't solve certain classes of problems with the C++ template language, eg. returning a T + U type. Maybe I'm just on my own in this line of thought, but C++ is difficult enough already. Is hard enough to read already. Do we really need an additional way of writing functions for this feature? Maybe just accept the limitation on move on. Explicit overrides and final are great in other languages like C# but they are basically a wet noodle in C++11, again they had backwards compatibility to be concerned about. These two additions are basically useless in C++ 11 because there is nothing 'enforcing' their use. So they are going to have the same problems as virtual had where someone, somewhere, forgets to add them. xgalaxy fucked around with this message at 20:35 on Feb 23, 2014 |
# ? Feb 23, 2014 20:30 |
|
xgalaxy posted:Explicit overrides and final are great in other languages like C# but they are basically a wet noodle in C++11, again they had backwards compatibility to be concerned about. These two additions are basically useless in C++ 11 because there is nothing 'enforcing' their use. So they are going to have the same problems as virtual had where someone, somewhere, forgets to add them.
|
# ? Feb 23, 2014 20:34 |
|
I would love an official "strict" mode which turns off a bunch of backwards compatibility stuff. R-value references seem ok. For most classes, the compiler-generated methods are good enough, so I only find myself actually writing code using them when doing something fancy i.e. rarely. I think of them like templates, they can make libraries nice and simple to use, but aren't much fun to write. I like override and final even though their use is sadly not enforced. Previously I always wrote 'virtual' on overridden methods even though it does nothing, just to make it obvious when you read the code. Override is a nice replacement for doing that.
|
# ? Feb 23, 2014 22:20 |
|
xgalaxy posted:I don't like how rvalue references were implemented. Given the restrictions of not breaking compatibility, etc. there isn't much different they could have done. But I dislike how you have to litter your code with std::move and std::forward, not to mention the horror of T&& in already verbose and hard to read templates. In addition its a easy to screw up rvalue references and end up doing needless copying because you forgot to add an std::move in your copy (move) constructor for example. xgalaxy posted:I think it is a bit of a joke how they introduced this new alternative style function syntax. Maybe instead of introducing a new way of declaring functions you just instead accept the fact that you can't solve certain classes of problems with the C++ template language, eg. returning a T + U type. Maybe I'm just on my own in this line of thought, but C++ is difficult enough already. Is hard enough to read already. Do we really need an additional way of writing functions for this feature? Maybe just accept the limitation on move on. xgalaxy posted:Explicit overrides and final are great in other languages like C# but they are basically a wet noodle in C++11, again they had backwards compatibility to be concerned about. These two additions are basically useless in C++ 11 because there is nothing 'enforcing' their use. So they are going to have the same problems as virtual had where someone, somewhere, forgets to add them.
|
# ? Feb 23, 2014 22:34 |
|
This should be a relatively simple question, I'm having trouble compiling a program I'm working on. I'm trying to move away from compiling in Dev C++ because it's a god awful IDE in my opinion and I'm trying to start using Visual Studio. However, visual studio doesn't like lines 43 & 53 (Indicated with arrows), why? This program runs fine in Dev C++ I assume this is a compiler issue?code:
|
# ? Feb 24, 2014 01:27 |
|
what the gently caress kind of source code is that
|
# ? Feb 24, 2014 01:29 |
Can you post the exact error message you get from the compiler?
|
|
# ? Feb 24, 2014 01:31 |
|
I'm guessing that neither linux nor __WIN32__ are defined.
|
# ? Feb 24, 2014 01:35 |
|
|
# ? Jun 1, 2024 23:11 |
|
Yeah, __WIN32__ seems to be a MinGW-ism. Microsoft documents that the define you should use is _WIN32.
|
# ? Feb 24, 2014 01:40 |