|
There is no straight forward answer. There is some good info here http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/ Also there is a scientific computing thread in SAL that might help.
|
# ? Dec 30, 2012 14:43 |
|
|
# ? May 30, 2024 02:14 |
|
peak debt posted:but I'm wondering if there's a better way to do this? What's the official way to handle things like this, I'm sure this shows up all the time in science but I never formally learned these things. Another option is to use a package that's meant for scientific computing, like MATLAB (FOSS clone is GNU Octave) or Mathematica. If you're writing something without a GUI or any library dependencies, that might actually be the easiest way to go.
|
# ? Dec 30, 2012 16:11 |
|
Mniot posted:Another option is to use a package that's meant for scientific computing, like MATLAB (FOSS clone is GNU Octave) or Mathematica. If you're writing something without a GUI or any library dependencies, that might actually be the easiest way to go. MATLAB won't help with precision issues, it uses the same doubles as the rest of us. Mathematica will handle it properly as long as you keep everything symbolic but that has other problems (like being fiercely slow). Both of these solutions have the ability to make GUIs or interface with external libraries. By far the easiest solution is just to deal with the fact that floating point numbers aren't quite real numbers and avoid comparing them for equality.
|
# ? Dec 30, 2012 16:42 |
|
Nippashish posted:By far the easiest solution is just to deal with the fact that floating point numbers aren't quite real numbers and avoid comparing them for equality. This is the real answer. I've found that in the great majority of situations you don't actually need to check floats for equality. You can almost always rewrite the algorithm in such a way that this isn't needed. The only safe check for equality with floats is if you're checking against a value you may have assigned previously. Peak debt, what is it you're trying to do here? Checking if abs(x) < some precision is an absolute last resort. HappyHippo fucked around with this message at 21:37 on Dec 30, 2012 |
# ? Dec 30, 2012 18:57 |
|
I'm currently working on this http://exar.ch/files/eclipse.svg One of the problems I was having is that the following code code:
|
# ? Dec 31, 2012 09:53 |
|
Hi there, I'm new to programming (particularly with questions about coding games and the like) and I've been looking around but I can't seem to find a thread for questions about Unity3d. Is there one around here I can refer to?
|
# ? Dec 31, 2012 21:54 |
|
peak debt posted:produces this http://exar.ch/files/math.html which isn't really supposed to happen (the loop ends one step later than it's supposed to). But you guys are right, comparing floats is quite unnecessary here, I can just make a loop that goes over an integer, then multiply the integer with a float and work with that number. N * a is much more accurate than a + a + ... + a (N times).
|
# ? Dec 31, 2012 22:54 |
|
Arus posted:Hi there, I'm new to programming (particularly with questions about coding games and the like) and I've been looking around but I can't seem to find a thread for questions about Unity3d. Is there one around here I can refer to? You could try the Game Development Megathread here in CoC, or the Making Games Megathread over in the Games forum. Both have plenty of Unity users hanging around who could help you out, usually the CoC thread handles programming specific techy stuff while the Games thread leans toward more general game dev discussion.
|
# ? Jan 1, 2013 08:02 |
|
I have never tied programming games, and it could be fun. But, I have heard a lot from games industry insiders, though most of them not engineers, and that industry sounds super lovely and not a thing I'd want to approach. I like playing games and making software that isn't games. I still do intend to mess around with Unity or UDK or something at some point though. The thing is, I'm a programmer, not a designer, and other than making and customizing engines, a lot of games is not programming.
|
# ? Jan 1, 2013 08:07 |
|
If you do want to take a whack at making games just for the hell of it, the One Game A Month shindig is just kicking off and it's not too late to get on board. The general idea is that you try to make one small game a month at whatever skill level you're at, and then try out some of the other people's games so that everyone gets some feedback and suggestions on how to improve. It's a low-pressure event that mainly serves as a motivation to get off your rear end and do something instead of promising yourself that you'll try out game dev someday. If you're a programmer just sign up as a programmer and let folks know you're looking for artsy/designer types. You'd probably pick up some collaborators pretty quick. We got a thread for it here. Also, they have 2,500+ people signed up for the thing. Holy crap. But yeah, by all accounts the game industry sucks rear end to work in. I just like doing game dev as a hobby because it tends to present a lot of interesting problems and touches a ton of different areas of comp sci. I don't even really care about making a 'fun' game most of the time, I just like the technical challenge of trying to make a realtime physics simulator that integrates CPU/GPU/sounds/UserInput along side whatever procedural level generation, AI, or pathfinding algorithms you happen to include. You end up learning a lot on each iteration and a lot of what you learn winds up being useful for more mundane dayjob programs.
|
# ? Jan 1, 2013 08:33 |
|
PDP-1 posted:But yeah, by all accounts the game industry sucks rear end to work in. I just like doing game dev as a hobby because it tends to present a lot of interesting problems and touches a ton of different areas of comp sci. That said, certain sectors of the games industry sucks rear end hugely if you're not in one of two positions: 1) you're the person with the idea or 2) you're given the idea and told you can do whatever the hell you want to bring it to life. The mobile games industry in particular is hosed to all hell because it's full of idea people with no concept of how to actually make games, and after a few bad experiences I've pretty much written it off as un-salvageable. Other sectors not so much, all the console/PC work I've done has been in great environments with good structure, and generally have better leadership than "I have a million dollar iPad app idea!". The Gripper fucked around with this message at 09:15 on Jan 1, 2013 |
# ? Jan 1, 2013 09:06 |
|
So I've gotten back into programming after a year and a half working on Oracle DB's and having all of my programming knowledge eroded away. I'm working on a project with a friend in Lua and I'm still trying to get used to the whole table based programming approach and metatables. Partly because the code called for one and partly because I wanted to get used to this language I've made a (very rough) circular array metatable: pre:{ __index = function (t, i) local idex = i % #t if idex == 0 then idex = #t end return rawget(t,idex) end , __newindex = function(t,i,v) if type(i) == "number" then end local idex = i % #t if (i == #t + 1) then idex = #t+1 end if (idex == 0) then idex = #t end rawset(t,idex,v) end }
|
# ? Jan 2, 2013 13:21 |
You need to be more careful around Lua's 1-based array indexing. (I still don't get why they thought that was ever a good idea.) Before you calculate the modulos, make sure you convert the 1-based index into a 0-based index, by subtracting one. Then convert back to a 1-based index afterwards by adding one again. Remember that the indexing values may not be numbers. You have an empty condition in the __newindex metamethod that checks for the value being a number and then does nothing on it. You should rather check if it's a non-number and then skip the circular array logic entirely. Since tables in Lua can't contain nil values, setting the value for an index in a table to nil is equivalent to removing the item from the table. So in your setter, check for the value being set being nil and then do whatever logic you want. (I.e. do you want to move existing indices down, or do you just leave a hole? Or maybe entirely disallow removing anything but the last element?) For that matter, how about the case of extending the array length? Lua code:
|
|
# ? Jan 2, 2013 13:52 |
|
nielsm posted:You need to be more careful around Lua's 1-based array indexing. (I still don't get why they thought that was ever a good idea.) Thank you for the prompt reply! The empty if in __newindex is a mistake. The end for that is supposed to be at the end of the function. Checking repl.it it seems that table.remove works correctly (it removed the index and decreases the other keys accordingly) and access works as expected afterwards. The array is only extended when you use table.insert(t,v) or try to set t[#t+1]. I'll use your advice to make it more robust however. Some things that get me are variables being global by default, for loops being very strange compared to C, and perhaps most scarily that the # operator isn't deterministic.
|
# ? Jan 2, 2013 14:29 |
Antinumeric posted:Some things that get me are variables being global by default, for loops being very strange compared to C, and perhaps most scarily that the # operator isn't deterministic. Yeah the "global by default" thing is a bit odd. Just make a habit of using "local" to declare variables. You can even have "local" names at top level scope! In that case, they are local to the file, essentially working like private symbols in OO languages, or "static" functions in C linkage. (Local variables are also slightly faster at runtime, since any global access involves a lookup into the _G table, while local variables become direct accesses into the interpreter stack.) Lua really has two flavours of for loops, numeric ones which act more like those in Pascal, and iterator-based which let you do all kinds of neat tricks. It's really just a different syntax. After a bit of testing, you are right that the # operator on tables is a bit weird. If I make an empty table then #t obviously returns 0. If I add a non-numeric index it still returns 0. Likewise, if I add index 0 is keeps returning 0. If I add index 2, it still returns 0 (presumably because there is no index 1.) If I then add index 1, it returns 2 (because now there is an unbroken series of indices from 1 to 2.) However if I then clear index 1 (by setting t[1] = nil) it still returns 2. I don't know if that is changed in Lua 5.2 though, I only have 5.1 on hand to test with.
|
|
# ? Jan 2, 2013 14:49 |
|
nielsm posted:Yeah the "global by default" thing is a bit odd. Just make a habit of using "local" to declare variables. You can even have "local" names at top level scope! In that case, they are local to the file, essentially working like private symbols in OO languages, or "static" functions in C linkage. (Local variables are also slightly faster at runtime, since any global access involves a lookup into the _G table, while local variables become direct accesses into the interpreter stack.) quote:Lua really has two flavours of for loops, numeric ones which act more like those in Pascal, and iterator-based which let you do all kinds of neat tricks. It's really just a different syntax. quote:After a bit of testing, you are right that the # operator on tables is a bit weird. If I make an empty table then #t obviously returns 0. If I add a non-numeric index it still returns 0. Likewise, if I add index 0 is keeps returning 0. If I add index 2, it still returns 0 (presumably because there is no index 1.) If I then add index 1, it returns 2 (because now there is an unbroken series of indices from 1 to 2.) However if I then clear index 1 (by setting t[1] = nil) it still returns 2. I don't know if that is changed in Lua 5.2 though, I only have 5.1 on hand to test with. Apparently the # operator only works as expected if you have a continuous numerical index starting at 1. eg In t = {[1] = 5, [2] = 6, [9] = 7, [11] = 9} any of 2,9 and 11 are legitimate values for #t depending on what order they were added and if anything was removed. It essentially checks for the first key that has no directly following key and returns that, saving the value until that key has a following key, whereupon it recalculates it. This took a bunch of testing to find out and is quite frustrating.
|
# ? Jan 2, 2013 15:17 |
Antinumeric posted:Yeah but touching the i value in a numeric for loop does some crazy poo poo. Like incrementing in place or something if you try and use something like t[i+1] = x. I didn't investigate enough before going back and replacing everything with while loops. Antinumeric posted:Apparently the # operator only works as expected if you have a continuous numerical index starting at 1. eg In t = {[1] = 5, [2] = 6, [9] = 7, [11] = 9} any of 2,9 and 11 are legitimate values for #t depending on what order they were added and if anything was removed. It essentially checks for the first key that has no directly following key and returns that, saving the value until that key has a following key, whereupon it recalculates it. This took a bunch of testing to find out and is quite frustrating. Oh, and for that, another thing that's very handy to know is that local variables of an enclosing scope can be captured into a function definition, and are kept between executions of the function: Lua code:
You can make "invisible" variables like that, for use with metatables and such.
|
|
# ? Jan 2, 2013 15:30 |
|
nielsm posted:So in short, it makes sense as long as you use the table as a dumb array and never fill nil values into the middle. Alternatively, override the __len metamethod for the table and do your own bookkeeping. Two things worth noting here: - # is only concerned with the array part, so you can freely use negative indices, floating-point indices, and non-numeric indices without affecting it. The only constraint is that you can't have gaps in the array. - __len on tables works only in 5.2. In 5.1 it is only respected on userdata, so you need to use something like newproxy to make use of it.
|
# ? Jan 2, 2013 15:43 |
|
I've got a pretty simple problem: given a sequence of numbers I need to find the first group of them that add up to some target number X, and if the group doesn't add up to X I need to "trim" the last one so that the total is X. For example, if the sequence is [2, 3, 5, 2, 1] and X is 12 then I want to return [2, 3, 5, 2], and if X is 11 I want to return [2, 3, 5, 1]. I'm sure this problem has a name. I'm not asking about it because it isn't easy to solve, I'm asking because I'm wondering if this is a potential opportunity to learn how to use monads for something. I've read blog posts and the wikipedia page and that sort of thing but I still haven't really grasped what they can do for me. I also don't know Haskell, which is kind of awkward. However, I'm doing this in F#, which also has monads, so I thought I'd ask. So: Is this an appropriate problem to solve with a monad? (We'll set aside whether doing so is worth it, because like I said I consider this a learning opportunity.) The reason the question occurred to me is that "seems only possible to solve imperatively" makes me think of monads. I don't know how correct that is - this is kind of a shot in the dark. If it is appropriate, I can start chewing on it (but any pointers in the right direction would be swell). If not, I'll know not to waste my time.
|
# ? Jan 3, 2013 01:45 |
Eh, "given a multiset of numbers, find the subset where the sum is X", is your problem as far as I can tell. You mention "remove the last" but your example contradicts that. A simple recursive solution would be: 1. Check if the list sums to X, if yes you have a solution. 2. If the list sums to less than X, it can't solve. 3. Create all the subsets of the list where one element is removed and recurse on them. It seems like a pretty simple combinatorial problem and I don't see why it should be better solved imperatively. An optimisation to the above algorithm could be using an actual multiset type that supports fast checking for another multiset being a subset and memoising known non-solving subsets. With memoisation like that an imperative approach might be okay. (E: The memoisation approach only works if the set is restricted to positive numbers.) nielsm fucked around with this message at 02:05 on Jan 3, 2013 |
|
# ? Jan 3, 2013 02:01 |
|
I think the 1 on the end of his example is a red herring - the 1 in the output is actually the 2 that's been chopped down to fit. I think. At least, that's all I can come up with that fits "easiest to solve imperatively".
|
# ? Jan 3, 2013 02:05 |
Jabor posted:I think the 1 on the end of his example is a red herring - the 1 in the output is actually the 2 that's been chopped down to fit. I think.
|
|
# ? Jan 3, 2013 02:07 |
|
nielsm posted:Eh, "given a multiset of numbers, find the subset where the sum is X", is your problem as far as I can tell. You mention "remove the last" but your example contradicts that. As I'm reading the problem, the elements of the collection are ordered, and the goal is to find a prefix (or prefix-like thing where the last one is made smaller). This is not bad to do with a recursive function, and I'm not sure exactly how a monad would help here.
|
# ? Jan 3, 2013 02:07 |
|
nielsm posted:So the shortest prefix that adds to at least X? That's my reading, yes.
|
# ? Jan 3, 2013 02:09 |
|
ultrafilter posted:As I'm reading the problem, the elements of the collection are ordered, and the goal is to find a prefix (or prefix-like thing where the last one is made smaller). This is not bad to do with a recursive function, and I'm not sure exactly how a monad would help here. Yeah, that's a good point. As I said, I was looking for a learning opportunity but I guess this isn't it.
|
# ? Jan 3, 2013 02:21 |
|
GrumpyDoctor posted:Is this an appropriate problem to solve with a monad? This doesn't strike me as a problem that has any particular need to involve a monad; there aren't any common monads that would seem to strongly resemble its structure. I suppose we can treat it as a nondeterministic computation in the list monad. code:
ShoulderDaemon fucked around with this message at 05:06 on Jan 3, 2013 |
# ? Jan 3, 2013 04:53 |
|
Anyone have any suggestions for organizing directory structures for external data to a project or group of projects? I recently got a new machine for work, so I thought before I start dumping everything into my home directory I try to get my data sorted properly. Generally, I put all my C code projects in ~/src/work, but for data external to the projects, I don't really know where to stuff it so that it's manageable. I have a lot of binary blobs that shouldn't be checked in to any of our repos, as well as PDF documentation and other related items, and I don't want my home directory to get polluted with useless items. I think I just need a nudge or a starting point to get me started.
|
# ? Jan 3, 2013 16:10 |
I prefer to have a "dev" directory under my home (called "dev" because it contains more than just source code!), then have project dircetories under that. For simple projects, the project dir may just be the source code checkout, but for things that need external documentation or such, the project dir contains several subdirs for different purposes, one of those will then be the source checkout.
|
|
# ? Jan 3, 2013 17:27 |
|
This is kind of a stupid question, but is there such a thing as FOSS that uses modern frameworks/patterns and isn't horribly massive and/or convoluted? Need to start getting into the world of FOSS in my spare time and I'd like to look at good examples of MVC/MVVM, IOC, Hibernate etc. implemented in a real-world scenario rather than always teaching and doing it myself (don't work for a company that's using anything terribly modern unfortunately).
|
# ? Jan 4, 2013 16:19 |
|
In IEEE floating point, if a > b then is b - a guaranteed to be less than zero? (Ignoring overflow issues, anyway. I'm not implementing a compiler, I'm just seeing if I can assume this particular thing in one particular case.)
|
# ? Jan 4, 2013 21:58 |
|
GrumpyDoctor posted:In IEEE floating point, if a > b then is b - a guaranteed to be less than zero? (Ignoring overflow issues, anyway. I'm not implementing a compiler, I'm just seeing if I can assume this particular thing in one particular case.) Not in any compliant mode. I asked an officemate, who noted that if the processor's in flush-to-zero mode, and you subtract a positive denormal from zero, you could end up with 0; but (for reasons like this) flush-to-zero is not actually IEEE-compliant.
|
# ? Jan 4, 2013 22:36 |
|
I'm having some trouble recursively extracting a ton of *.7z files. Here is my directory structure:code:
I'm currently using: code:
|
# ? Jan 5, 2013 02:18 |
|
Henrik Zetterberg posted:I'm currently using: code:
|
# ? Jan 5, 2013 10:40 |
|
nielsm posted:
This won't work, unfortunately -- newindex will only be called if t[i] doesn't already exist, so v == nil can only be true if there's nothing to remove.
|
# ? Jan 5, 2013 15:48 |
mjau posted:This won't work, unfortunately -- newindex will only be called if t[i] doesn't already exist, so v == nil can only be true if there's nothing to remove. Huh. Either that changed in Lua 5.2, or Lua 5.1 wasn't documenting it properly.
|
|
# ? Jan 5, 2013 16:06 |
|
nielsm posted:Huh. Either that changed in Lua 5.2, or Lua 5.1 wasn't documenting it properly. I think it's been like that since 5.0 (__index works similarly), but it's easy to miss, yeah.
|
# ? Jan 5, 2013 17:00 |
mjau posted:I think it's been like that since 5.0 (__index works similarly), but it's easy to miss, yeah. Hmm solution: Keep the actual table empty and hold the real data in a hidden table, either from an upvalue in the constructing function or in the metatable!
|
|
# ? Jan 5, 2013 17:13 |
|
nielsm posted:Huh. Either that changed in Lua 5.2, or Lua 5.1 wasn't documenting it properly. It's been documented correctly since 5.0, when it was added. Metamethods in general are fallbacks, not overrides. The usual approach is to use an empty proxy table (or newproxy() userdata) so that __index/__newindex always trigger.
|
# ? Jan 5, 2013 17:56 |
|
So I don't regularly do any programming but I have taken a few intro comp-sci courses in school. I've learned basic syntax and stuff in a few languages. My question is: What language should I use for reading/manipulating text documents? I'm looking for a simple way to have a program read an input.txt and give me an output.txt. I did something like this in an algorithms class in C++, but our professor helped us through setting that up and I've since forgotten. I'm asking because I'd like the simplest way/language to get started, I don't need anything fancy at this point.
|
# ? Jan 5, 2013 19:54 |
|
|
# ? May 30, 2024 02:14 |
|
Use Python.
|
# ? Jan 5, 2013 20:10 |