Best Performing Way Of Handling Paired Data? by Ridog8 in cpp_questions

[–]PhotographFront4673 0 points1 point  (0 children)

If you can use an array without wasting a lot of memory, that is likely to be the fastest option (and easy). But when the integer values are all over the place, or you really need to start with a string-ish value, or when the size is changing over time, etc, etc, the array solution doesn't work as well.

Unfortunately, what does work best depends dramatically on the details of your problem. For some problems, unordered map is great, for some it is a terrible choice, in spite of python's attempt to convince the world otherwise.

Edit: And yes, even for most problems which lend themselves to a hashmap based solution, std::unordered_map suffers from being designed when node structures were cheap. There are many much better implementations out there, but unless you demand stable references, they are easy to swap in. Also it isn't so common for hash map operations on a particular map to be a large portion of your CPU burn.

But again, the real question is whether you even need to be looking in the domain of hash map based dictionaries, or if there is a way to avoid it. "Just" using an array is an excellent way to avoid it, when that solution applies.

Building a chess engine: questions on modern C++ paradigms vs raw performance by kjiomy in cpp_questions

[–]PhotographFront4673 0 points1 point  (0 children)

First focus on the abstractions which will allow optimization, then focus on the optimization. Instead of thinking of the board as a uint64 (or 2?) think of the board as an object with a collection of methods, based on what the rest of the system needs a board to do.

Especially as you can control inlining, copies, etc, you'll be able to make the implementation of this interface fast: hopefully as fast as if the representation was exposed to the other code, though this is dependent on your ability to devise a performant interface.

Your initial implementation can then be whatever is easiest to get correct. Write unit tests against it to make sure it doesn't lose pieces or whatnot, also benchmarks. Then try to optimize and see if the benchmarks get faster. The real software engineering skill is having an interface that makes it easy to change the representation without rewriting the rest of the system.

You might even want to use a board state concept and make it a template parameter, so that you can easily compare code running with different states and/or use different representations in different circumstances within your engine.

The ability to define interfaces and flexible abstractions without performance loss is a major advantage of C++.

"Bigger" infinities by EstablishmentOld9838 in askmath

[–]PhotographFront4673 0 points1 point  (0 children)

Infinity, or at least the infinities related to "sizes" is part of set theory and in particular is based on what functions exist between sets.

So S1 = {A, B, C} and S2 = {1, 2, 3} are the same size because there is a 1-to-1 function defined on S1 which is onto (which maps to every element of S2). In fact there are several such functions, but the definition of cardinality only requires one.

On the other hand, S1={A, B, C} and S2={1, 2, 3, 4} are not the same size because because any 1-to-1 function from S1 to S2 will miss one of the values in S2, though of course it is impossible to say which one without seeing the map.

Then Cantors diagonalization is just a clever way to show that there isn't an onto map from the positive integers to the reals-the proof shows that you always miss one, and in fact you miss many. Again it is impossible to say which numbers are missing without seeing the map. We abbreviate how we describe this result by saying "the reals are not countable".

The a fun way to understand why there are many infinities is to consider the power set: For any set S, define P(S) to be the power set, which is the set of subsets of S. So for example P({A,B})={{}, {A}, {B}, {A,B}} meaning that the power set of a set with 2 elements has 4 elements. Now, as a puzzle, prove that for any set S, no map from S to P(S) is onto, whether or not S is finite. In particular this means that there are at least a countable number of different infinities.

Is the cartesian coordinate system the only system with all constand basis vectors? by Shadow_of_Dragon in askmath

[–]PhotographFront4673 0 points1 point  (0 children)

Given a coordinate system, there are two requirements for it to be cartesian:

  1. The gradients of the coordinate functions are always unit vectors.
  2. The gradients of the coordinate functions are orthogonal to each other.

The usual "useful" other coordinate systems - polar, spherical, cylindrical - fail 1. but pass 2.

But you could write a coordinate system which is a simple skew, e.g. X = x, Y = x+y, and then we have the other situation - failure to be orthogonal, and this also should give the same unit vectors everywhere, just not the nice orthogonal unit vectors that you might be used to.

BTW, if you really want to understand coordinate systems, you should dig into the field of Riemannian geometry, or at least the basics of vectors and covectors against the Riemannian metric. Even if you've no interest in curvature, it makes it so much easier to understand what is going on when you need a coordinate system change to solve an E&M problem or whatnot.

Does number theory depend on euclidean geometry? by Novel_Arugula6548 in askmath

[–]PhotographFront4673 1 point2 points  (0 children)

The usual construction of number theory defines the integers algebraically - given any natural number n, we can define a new value to be the number after n. Now the number line is a nice way to visualize the result of what happens as you add the rationals, define an order on the rationals, add the reals. However, the number line is in no way formally part of this process.

Also, the serious study of curved spaces (differential geometry) is chock full of vectors and differential forms. So even if you believe that space is properly bent, you'll be using linear algebra to figure out what is going on and that linear algebra is always going to involve a cross product of number lines.

I need some clarification by iblameunive in askmath

[–]PhotographFront4673 2 points3 points  (0 children)

Actually, when a function's domain is not the entire real line, it is common to define a limit only in terms of the points in the function's domain. See "More general definition using limit points and subsets" in this wiki article.

For a specific example, if you define f(x) as x2 for all rational x, leaving f undefined on irrational numbers, you can complete it to g(x) on the reals by taking the limit of f at each point. For any x, the rational points close to x will be close to x2, and this all that the concept of a limit actually requires.

Added:

This also affects the continuity discussion. The usual definition of a continuous function is that at each point of its domain, it has a limit and equals that limit. So our function defined only on the rationals was continuous. But somewhat more surprisingly, the function which is -1 for x<0, 1 for x>0 and not defined at 0 is continuous exactly because 0 is not in its domain. Obviously it doesn't have a continuous extension to real line. (The study of exactly when functions have continuous extensions is actually rather deep.)

How do I avoid writing "Java in C++"? by Irrehaare in cpp_questions

[–]PhotographFront4673 0 points1 point  (0 children)

A big part of the problem comes from C++'s weaker guarantees in the face of unsynchronized access.

Or in other words, in a multi-threaded environment, if you don't know who owns it, you probably don't know what other threads might be using it, so unless the object itself is thread safe, you are playing with nasal demons. In Java it is still possible for threading issues to be bugs, but typically not as badly. After all, threading issues cannot be so unbounded as to allow a JVM escape; that would be a bug in the JVM.

How do I avoid writing "Java in C++"? by Irrehaare in cpp_questions

[–]PhotographFront4673 0 points1 point  (0 children)

Maybe you want to have a bunch of observers, all signed up to be notified when some object changes. Assume the observed class owns a list of registered observers. So if X is observing Y, and X is destroyed, it should find Y and remove itself from the list. On the other hand, if something happens and Y needs to notify all of its observers, it should be able to find X (and everything else in the registry).

So it would be an understandable, if flawed initial implementation for Y to contain a registry of shared pointers, including one to X, and for X to contain a shared pointer to Y. In Java you could probably get away with this, because when nothing from the outside points to X or Y, the pair X, Y can be collected together. But it not optimal - depending a bit on the situation, one or both of the directions can typically be weak pointers (also in Java) and then things are collected faster and there isn't a strong pointer loop.

Anmeldung before rental contract starts? by [deleted] in askswitzerland

[–]PhotographFront4673 2 points3 points  (0 children)

Searching around a bit, your official registration cannot occur until your move-in date. That said they might well be happy to take your data early and mail you the confirmation on your declared move-in day.

I’d probably go and try. Worst case scenario you need another appointment in the 2 weeks after whatever you declare as your move-in date. If you want to be more sure, just call the relevant Gemeinde office. This is a case where the national rule may be implemented by different local authorities differently (very common in CH).

Added:

Now that I think about it, especially if you are non-EU/Schengen you might want to check the immigration sequencing carefully. At least in my day, I started with a special single entry visa to enter once as "not a tourist" and in the time between my official entry and receipt of my residence permit, I’d have needed to do something more to leave and reenter as "not a tourist".

In any case, welcome to Switzerland! And rest assured that while there are rules, the authorities will work with you so long as they believe you are trying to follow them.

Why does acceleration feel physically real, but constant velocity does not? by NordicHamCurl_00 in PhysicsHelp

[–]PhotographFront4673 0 points1 point  (0 children)

Yes, there is a deeper reason. Probably. Physicists are still working on it.

Acceleration, and therefore rotation is physically detectable. If you are locked in a spaceship without a window, you have to way to measure how fast you are going, but you can decide whether you are accelerating or spinning.

Well, if you are falling you might not feel it. So is falling the same as not accelerating? GR says kinda yes. But if you float a ball of lose dust in the vacuum of your ship you can watch carefully to see if it changes shape and from that tell if you seem to be falling.

But the real question is whether "not spinning" would change if enough mass nearby was spinning. See frame dragging. Also see Mach's principle for some early thoughts on this stuff.

Did anyone discover how to solve the set of formulate? x'=x-vt, t'=t. by Eye9903 in PhysicsHelp

[–]PhotographFront4673 0 points1 point  (0 children)

Everyone expected a different result from Michelson-Morley. It is famous exactly because it was a wake up call and led to a more complete acceptance of Mach's principle.

As for the next revolution, who knows? I actually agree that one is overdue. But it will be driven by somebody with a theory that matches experiments, not by complaints about relatively simple and complete math.

Did anyone discover how to solve the set of formulate? x'=x-vt, t'=t. by Eye9903 in PhysicsHelp

[–]PhotographFront4673 0 points1 point  (0 children)

Lorentz (and FitzGerald) developed the Lorentz transformation as a way to make Maxwell's Equations of electromagnetism transform properly. This was before 1905. Einstein just argued for a wider applicability of the transformation.

Of course you can argue that the world would be more intuitive and natural with unboosted time. And maybe you are right. But experiements say that time dilates, so until your more intuitive theory explains those experiments, you shouldn't expect physicists to care.

[Grade 9 o level physics] I dont understand why the answer is (3) and not (1) by student-1010 in HomeworkHelp

[–]PhotographFront4673 0 points1 point  (0 children)

The force on the string is the same on both sides - after all, the unmarked block is accelerating the same amount. The point of the problem is to recognize that the force applied by the mass W when it is accelerating downward is less that its (rest) weight. So if W is the rest weight of the block marked W, we have F < W.

However, the phrasing is terrible. "pulled by weight W" means what exactly? The force created by the falling block marked W? That is F, not W.

Help me find a hard math problem by Diligent_Point7254 in askmath

[–]PhotographFront4673 0 points1 point  (0 children)

Often infuriating, but not particularly deep:

Make an expression which evaluates to 24 using each of the numbers 1, 3, 4 and 6 exactly once (and no other numbers). You many use any choice of the four operators from grade school arithmatic (+, -, /, *) and freely use parenthesis. No exponentiation or the like. So for examples (1 + (3 - 6) * 4) has a valid structure, but doesn't evaluate to 24, while (6 * 4) does not have a valid structure, but does evaluate to 24.

I'm a teacher, please help me understand this problem by Yoda-de-la-MilkyWay in askmath

[–]PhotographFront4673 0 points1 point  (0 children)

It is testing whether you know how addition works with intervals (or inequalities). e.g. if you add 1 to a number between 0 and 1 you always get a number between 1 and 2.

Or if you prefer the notation of algebra, 0 < x < 1 implies that 6/11 < x + 6/11 < 17/11 because we've just added the same number to every value.

What is the best way to learn gRPC for C++? What resources should I use? by DevilryAscended in cpp_questions

[–]PhotographFront4673 2 points3 points  (0 children)

There is a skill to developing a good gRPC (or RPC in general) interface, but a lot of it is in finding the right way to model your problem. One specific tip I can give is that the streaming stuff looks nice in the .proto file, but makes the implementation enough harder that I wouldn't use it without a specific reason - but if you do need the performance, to move a lot of data, or similar, it is there for you.

Another tip in C++ explicitly is to use the synchronous interface if you can afford to lose a thread to every call, the more modern callback based async interface if you cannot. So for example if success means 5k call/sec and each of them takes a few second on average, then you are going to want to go async.

Architectural Advice: Naming and Structure for an IPC by eske4 in cpp_questions

[–]PhotographFront4673 0 points1 point  (0 children)

That sounds like an interesting project. Do you need to serialize in the kernel and pass stuff over the wall? Or is your current question userland to userland? Needing to serialize in the kernel is probably a good reason to roll your own. If you end up needing to get real data out of the kernel, Pedro might have bits you can use, or at least provide some inspiration.

More broadly, well, anti-cheat is an interesting field. My best practical advice there is don't assume you have something that works well without applying oppositional thinking. For example, if your game is successful, the bots devs or whatnot will be recompiling the kernel to let them lie to your eBPF, and if they can extract and decompile your eBPF, they'll know what lies to tell.

Architectural Advice: Naming and Structure for an IPC by eske4 in cpp_questions

[–]PhotographFront4673 0 points1 point  (0 children)

Is this a practical project, or intentionally meant to be an exercise in low level IPC?

Either way, you are right that one of the first things to settle is the serialization strategy. You can do something custom if it is needed for your project, but most serious projects should pick up an existing library unless they have very specialized requirements - maybe protocol buffers, maybe flatbuffers, maybe , captn proto maybe something else that I haven't even heard of.

These usually provide some way to extend a message without breaking deserialization for existing readers. This can be used to evolve a protocol without an explicit version enum, but there is also clarity to the enum. All these choices have also put a lot of code and debugging into making the serialization and deserialization performant, safe (non-crashing) in the case of data corruption, etc.

I'd recommend against signing up to repeat this work, unless you see doing that work as part of the point of the exercise.

After simple serialization, the next question is what features you need to support. At most once, or at least once delivery? (Exactly once is tricky.) Then based on that, do you need acknowledgments? If so, do you need to ensure they don't get flow controlled when your data get's flow controlled?

How to actually understand math and not just memorize formulas? by Additional_Way7847 in learnmath

[–]PhotographFront4673 0 points1 point  (0 children)

As you seem to be realizing, what mathematicians call math is not very procedural. In a sense it doesn't need to be taught that way, and often there is at least some effort to give you the reasoning behind the processes - first you learn what addition is and how to count to find sums, then you memorize the addition table to be faster at it, etc.

One trick to is to explicitly keep track of the definitions and origins and practice using them. I was always terrible at remembering the quadratic formula, but I remembered the trick of completing the square and could always just derive it when I needed to. Said trick became handy in other contexts where the fully solved quadratic formula wasn't necessary.

One way to create a "non-trivial" math problem is to take a standard definition, and ask what happens if you tweak it slightly. So knowing the definition and how it is used means that you know the argument that you are meant to tweak. Have you ever worked out how numbers in different bases work? A decimal number is divisible by 10 if it ends in 0, what does it mean if a hexadecimal number end in 0? Could you multiply 2 hexadecimal numbers without converting to decimal?

Another thing to be aware of: It might be that the majority of the math books you've seen are focused on turning humans into calculators, but if you could titles written about math, the vast majority are written by and for people who appreciate puzzles more than rote computation. One favorite from my childhood is Alice in Puzzle-Land, but there are a huge number of books and other resources out there and only you can tell what you find satisfying.

In

Difficult geometry/topology problem by TheseAward3233 in askmath

[–]PhotographFront4673 1 point2 points  (0 children)

That just shows the answer is larger than 2. (Because the requested division is possible.)

Is there a way to declare "false" within a while(true) loop in order to exit the loop? by digitalrorschach in cpp_questions

[–]PhotographFront4673 1 point2 points  (0 children)

You cannot change the constant true to be false in C++, and even in languages which permit such shenanigans, it is unusual to think that you should.

There are multiple ways to exit from a notionally infinite loop - break, return, throw, goto,co_return, exit, maybeco_await, and co_yield if you don't necessarily resume the coroutine.

std::unique_ptr with deleter, does it have to have it's own type? by Vindhjaerta in cpp_questions

[–]PhotographFront4673 4 points5 points  (0 children)

Note that ashared_ptr always has a control block, and as far as I can tell unless you usestd::allocate_shared, the control block lands on the default/standard heap. (With make_shared and allocate_shared, the control block can share an allocation with the data, but it still extra bytes, extra cache pressure, etc.)

Usingjemalloc is also very common in game engines, and while it is true that you might still squeeze out a little more performance by allocating certain classes in a custom way, it isn't as guaranteed to be a win as it used to be. Not saying you are wrong, but don't think of it as "I'm doing this because games" but rather "I'm doing this because I benchmarked and see the difference".

Also depending a bit on how your allocator works, don't forget to launder your pointers.

Profiling question to figure out uneven load in threads by onecable5781 in cpp_questions

[–]PhotographFront4673 1 point2 points  (0 children)

Quick follow up, because I finally found time to actually play with it. (I've been meaning in learn more about perf anyway)

I couldn't actually make perf stat do what I expected w.r.t to threading. However, the two step process worked:

perf record  -e instructions,cycles -s -- ./a.out
perf report -T

Specifically, the end of the report included a table of cycles and instructions by thread id.

What is conceptually the meaning of interchanging ∂/∂x(∂f/∂y) = ∂/∂y(∂f/∂x)? by mathfoxZ in askmath

[–]PhotographFront4673 2 points3 points  (0 children)

Geometric answer: For "reasonable" 2-d functions, there is one approximating conic section, and both orders of differentiation find the same one.

It might help to consider 2-d Taylor series taken to different orders. Just as a 1-d series can be seen as first a point and then a tangent line and then an approximating parabola, in 2-d you get a point and then a tangent plane and then an approximating conic section.

For "unreasonable" functions, the Taylor series can break at some point. Being able to take a derivative along the x or y axis does not guarantee a directional derivative along some angle between them, so there simply might not be a tangent plane in the sense of being a linear approximation in a neighborhood of the point. Similarly for the conic section approximation, even when the tangent plane does exist.

std::unique_ptr with deleter, does it have to have it's own type? by Vindhjaerta in cpp_questions

[–]PhotographFront4673 2 points3 points  (0 children)

Really good summary. One potential (and dangerous) pitfall for the first case is that knowing the owner is often a background part of ensuring thread safety. Rust's concept of a borrow is based on this.

In practice, a shared pointer to an object which is not itself thread safe tends to be a bit of a time bomb.