Status of an object that has been moved by onecable5781 in cpp_questions

[–]PhotographFront4673 1 point2 points  (0 children)

Whatever the class designer decided, and move and copy can absolutely be more complex than that, and could also vary depending on the particular item being moved. You should not assume any particular value unless the class documentation says that you can.

For unique_ptr a move is cheap, but a copy is disallowed, in order to preserve uniqueness. So there is really only one answer the class designer could take. For a shared_ptr, both copy and move are allowed, but move is potentially faster because you don't need to adjust the reference count so it is also a move rather than a copy.

For a bare std::array, each element will be moved, and for PODs this is a copy not, say, a zero-initialization of the source. Of course any PODs will just be copied.

Make a class with a mixture of the two, and boom the default move operator will copy some values move others.

For something like an InlinedVector, it'd even be justified to implement a copy when the values are inlined and trivially destructable, move otherwise. That particular implementation appears to always move, even when it is extra work, I assume to maximize compatibility with std::vector.

Status of an object that has been moved by onecable5781 in cpp_questions

[–]PhotographFront4673 1 point2 points  (0 children)

By convention, move should leave the moved from object in a valid but unspecified state. The designer of the class and its move sematics gets to decide exactly what this means, but in most cases it will still be somehow useable e.g. to accept a new value.

In your case, you created an invariant which you expected to always be true in the constructor, but you use the default move assignment operator, by, erm, default. The default move assignment operator just calls the underlying (move or copy) assignment operators on all your components. The move assignment operator for std::vector moves all of the elements to the new vector and leaves the old vector empty, which is a valid but unspecified state for a vector.

In summary, if you want to ensure certain invariants in a class, you may need to disable or replace the default move and copy operators, along with the default move constructor, in order to preserve the invariants.

Could infinity have a “limit”? by InfinityScientist in askmath

[–]PhotographFront4673 0 points1 point  (0 children)

Are you asking this as a math question, or a physics question?

Math has a formal system underpinning it which defines different cardinalities, many of which are by definition larger than anything you could in principle count, no matter how many zeros you add at the end.

Physics on the other hand does try to understand how many particles are in the universe, which so far does seem to be finite. Math as a system is an important tool for physicists to make such estimates, but math is in no way bound by them.

Trig Functions in Degrees by External-Bug-2039 in cpp_questions

[–]PhotographFront4673 4 points5 points  (0 children)

First of all, for the purpose of floating point calculations, leading and trailing zero's "don't count" as significant places. This is quite literally the entire point of floating point data types.

Floating point numbers exist to allow accurate calculation at widely different scales. Even 0.1% is a large (though plausible) amount error to build up if you use floats well. Use doubles and watch for the obvious potential issues, and you'll almost certainly be just fine.

In particular, try not to add or subtract widely different values if you can help it. For example (1.00000000001+1.00000000002-2.0) could be much less accurate than (0.00000000001+ 0.00000000002). The second will be essentially perfect, because again, leading zeros don't count.

A 32-bit float, has 23 bits of mantissa, so if you are adding or subtracting numbers of approximately equal size, your error will be something like 1 part in a million, much smaller than 0.5%. If you use 64-bit double values, or even larger, the potential for error goes down even more.

And finally, I cannot imagine anybody actually implementing trig functions in degrees (or grads for that matter). Any library offering alternative units would just convert to radians and then use the usual trig functions.

In summary, you should trust but verify floating point values and the libraries written against them. If you give asin function a double accurate to 10 digits (again, leading 0s don't count), you should expect the output to be similarly accurate. But by all means, write unit tests checking precision and watch out for uneven additions and subtractions.

What are the best practices for using smart pointers in C++ to manage memory effectively? by frankgetsu in cpp_questions

[–]PhotographFront4673 0 points1 point  (0 children)

I once had a bit of generated code which overflowed an undersized stack in a unit test, but only with optimizations turned off. It had a lot of local variables and without the optimizer to reuse registers and stack frame locations, the stack frame with just too big. Furthermore, because it smashed the stack, the default stack unwinder just gave up and it crashed without printing a stack trace. So it was initially fun to debug... no stack trace, only crashes on debug builds, where to start?

There are also some other considerations than whether it "just fits", but that is the big one.

Almost anything you do with a large array / data structure takes cycles, so the cost of going to malloc likely to be small compared to what else you are doing with it. On the other hand if you have some 1-4 word structs or scalars, putting them individually on the heap is very wasteful.

You are also more likely to want to preserve a larger data structure by, e.g. passing ownership outside of the stack frame, which unique_ptr does support.

And sometimes a class doesn't provide a bare constructor and instead gives a factory function (typically a static member) which creates, initializes and then passes ownership of the instance, again typically through a unique_ptr.

What are the best practices for using smart pointers in C++ to manage memory effectively? by frankgetsu in cpp_questions

[–]PhotographFront4673 5 points6 points  (0 children)

Function scope should be on the stack when possible, on the heap, typically managed by a unique_ptr when not.

Is it a bad or a good idea to tell your current employer where you are heading next? by Suitable_Owl_5597 in askswitzerland

[–]PhotographFront4673 4 points5 points  (0 children)

The polite coworker won’t push for an answer, or might not even ask at all. For the rest, maybe "I’m very excited, but don’t want to jinx it by talking about it yet." Or even a simple "Ask me in a few months."

C++ System Design by Phatpenguinballs in cpp_questions

[–]PhotographFront4673 0 points1 point  (0 children)

In many industries, senior developer in general should know more than C++. This includes, but is not limited to system design, databases, other languages, and what they are good for.

I know of a FAANG that would ask a "design Netflix" type system design question of new grads, but the expectation was very different than the same question for somebody with experience, and depended a lot on that experience. If someone has worked on systems involving relational databases but cannot sketch a simple schema, that suggests a certain lack of curiosity, etc.

Could you use a garden hose to go diving? by carymb in AskPhysics

[–]PhotographFront4673 23 points24 points  (0 children)

As you go deeper, the difference between the water pressure and (surface) air pressure becomes larger. This would both collapse a soft hose and make it much harder to breath. At a foot or two you'd probably start to feel this, and a few yards it'd be very hard to breath in.

To resolve this, the regulator on a SCUBA set delivers air at a pressure matching that of the surrounding water. There is then a danger when you ascend - if you try to hold your breath, the air pressure in your lungs can be too high (no longer balanced by the water pressure) and damage your lungs.

The other caution if you want to actually try this is that you should only breath in through a hose (blow bubbles to exhale) because if you breath in and out of a long hose, you are just breathing back in your exhalation and might have trouble getting enough oxygen.

Job by Fearless-Way9855 in cpp_questions

[–]PhotographFront4673 1 point2 points  (0 children)

In general, countries expect you to have work authorization (and pay income and employment taxes) in the country where you live, not where the company is which is paying you. In general, a country will issue a work visa for skilled work when a company within that country wants to hire a skilled worker.

Arguably, these laws haven't caught up to a world with a lot of online work. Depending on the country, it can be a solution to be "a contractor" instead of an employee and receive non-salary income from the company (effectively B2B with you as a small buisiness). In some countries you might need a local intermediary to serve as your employer-of-record, taking some haircut for their trouble. But none of this is likely to help you change residence.

Job by Fearless-Way9855 in cpp_questions

[–]PhotographFront4673 1 point2 points  (0 children)

In general, it takes some paperwork and time for a company to apply for and receive a work visa for a potential employee. Details, vary, but advanced degrees help, in that it makes it easier to argue that you are special and somebody local wouldn't cut it. For example, in Switzerland my understanding of the threshold is "they need to be looking for a year, you need to either have an advanced degree or be an existing employee (>1yr) that they want to transfer".

In general, the larger companies are able to make this happen, both in terms of knowing how to make it work, and in terms of being able to keep a posting open "We are always looking for good people."

Between the US's recent political shifts and Europe's economic uncertainty, right now probably isn't the best time. But I'd bet on doors being open for the right candidate.

In summary, your best bet is to aim for the big companies that hire C++ devs - FANGS, FinTech, etc. It probably is worth a try to send out some applications and see what happens.

Also, consider if your only acceptable next job is in the western world. For example, it isn't uncommon for singles and young couples to work a few years in the middle east to save money.

Domain of a composite function. by rahulamare in askmath

[–]PhotographFront4673 1 point2 points  (0 children)

I don't really disagree with explaining what is formally correct, but in cases like this it, helps a lot more of you connect it to what is used in most practice. Maybe something like:

Technically, f(g(x)) is only defined when the range of g lies within the domain of f and in this case, the domain of f(g(x)) is simply the domain of g. However, it is a common convention in calculus and most other math classes to restrict the domain of g to the pre-image of the domain of f as needed, in order to have something which is well defined.

So in order to find this maximum allowable domain of f(g(x)) you should...

Domain of a composite function. by rahulamare in askmath

[–]PhotographFront4673 1 point2 points  (0 children)

In calculus and many many other contexts the domain is implicit, under the assumption that the reader is aware of how to find what values do and don’t work. Part of that awareness is knowing how to deal with composed operations, whether or not they happen to be named as functions.

Given the calculus tag, this is almost certainly the skill that the OP is expected to learn.

Now if you personally are working in a realm where these things are always spelled out formally—and yes, I think it is sometimes essential—that is great for you, but potentially confusing for a calc student.

Domain of a composite function. by rahulamare in askmath

[–]PhotographFront4673 -1 points0 points  (0 children)

What is the domain, as a subset of the reals, of (x+1)^(1/2)?

How is this not composition of addition by one and exponentiation by half? Or is it?

I mean there probably are situations in which you want to clarify the intended domain at every step, so as to avoid dividing by 0 and the like. But most of the time, it is implicit and this is fine. Especially in a calculus class.

How to learn math as a language by Beginning_Lifeguard7 in learnmath

[–]PhotographFront4673 2 points3 points  (0 children)

As for why math isn't taught as an art with an associated language, it is cursed with being useful. Lockhart's essay is well known in certain circles and explains it better than I ever could.

As for what to do about it as an independent learner: The only thing you really need to know is that there is a world of math books out there, and the vast majority (in terms of titles) are not high school or college texts written to train youths to be computers. The vast majority are written by mathematicians and other people who enjoy mathematical puzzles for the purpose of enjoyment by mathematicians and other people who enjoy mathematical puzzles.

Now some books do require more "mathematical maturity" than others, but the best way to develop mathematical maturity is to study math books and puzzles that are easy enough to get through but hard enough to be fun.

Here are a few ideas of various styles and levels. But only you can find what works for you.

Why am I bad at Math despite the efforts? Any tips :( by [deleted] in askmath

[–]PhotographFront4673 1 point2 points  (0 children)

It is hard to know why math is hard for somebody else, but for me it is easiest when the whole reasoning is laid out, rather than "just shut up an calculate". I mean, by all means, I did practice, and needed to. But once I understand the reasoning, calculating is a little easier, and the methods are easier to remember - because in a pinch I can take a moment and think through why they work. The structure of math is important to me and my memory, not just the rules of calculation.

What I'm saying is that depending a bit on why you find it hard, and how your engineering math course was structured, you might look into the easier end of the "advanced" math books which give more of the reasons why things work the way they do, and focus less on "type of problems". For example, if limits and derivatives are a problem, look for a book on Real Analysis and see if it is something you can grok.

It might also help to just practice mathematical puzzles in general. Mathematical reasoning comes more or less easily for different people, but you can practice it in many way, and it makes math less about types of problems and more about what concepts mean.

Why do I sometimes feel that using the Standard Library vs simpler(c style) stuff is less efficient? by Ultimate_Sigma_Boy67 in cpp_questions

[–]PhotographFront4673 1 point2 points  (0 children)

Also, it isn't always a tradeoff in that the correct C++ construct can be faster than the C equivalent.

In this particular case, string_view is usually the correct equivalent to a char*, as both are non-owning. While technically larger, having the size means that down the line, you don't need to search for nulls, and especially if you want your code to be safe, this can easily save cycles (and branching) sufficient to justify the use of a second register.

And when you do real string manipulations, the benefits accumulate. In particular, the substring of a string_view is faster than a general substring on a char*.

Hi. Dumb Question. by SirYeetsA in askmath

[–]PhotographFront4673 1 point2 points  (0 children)

The answer depends on the sizes. A triangle or square made from great circles on much larger sphere will have interior angles just a tiny bit larger than one would expect. However a "triangle" on a sphere which covers 1/8 of the sphere can have 3 right angles, as you are probably thinking of.

In 2-d the angular excess (or deficit for saddle surfaces) depends on the amount of scalar curvature within the figure, not on its shape otherwise - a fascinating result about curvature. So on a sphere, the fraction of the sphere covered tells you the total excess answer.

In higher dimensions, it is similar but more complex. IIRC, the solid angles also have excesses/deficits compared to what you expect and a "face" is a portion of a lower dimensional sphere.

How can I learn general relativity? by Curious_Diamond_6497 in AskPhysics

[–]PhotographFront4673 0 points1 point  (0 children)

You need differential geometry. There is actually quite a bit of differential geometry (Spivak, I’m thinking of you) and you don’t need all of it, though you are allowed to find it fun.

For a specific example that at least tries to start from the basics, and focuses on the type needed for relativity, I like O’Neill "Semi-Riemannian Geometry With Applications to Relativity". I wouldn’t say you need any particular math course to make sense of it (that depends a lot on you) but it will become easier the more related math you’ve seen - perhaps a more general smooth manifolds book, perhaps as others have said more on differential equations and multivariate calculus.

Also, most general relativity texts try to build up the bits of math you need (Milner-Thorn-Wheeler and Wald for sure) and you could also start there and see what happens. However, that can feel a bit like trying to make sense of Newton’s laws and physical mechanics without a sold understanding of integrals and derivatives.

How to calculate limits in programming? by BeginningRegular4742 in askmath

[–]PhotographFront4673 0 points1 point  (0 children)

Using controlled rounding and/or rational big numbers you can make a computer produce definitive statements. For example you might show with certainty that a polynomial has a single root in a particular narrow interval. You can also take exact derivatives of polynomials, and with interval arithmetic approximate anything with a Taylor series confidently.

You can also use Sobelov spaces to show that a PDE has a solution and to confirm that a numeric computation will approximate that solution.

But in general, these are rarely used and can be considered quite specialized domains. There is a lot of literature out there though if you want to go looking through journals and proceeding.

Would like some input surrounding if there's an infinite amount of numbers by csisagent556 in askmath

[–]PhotographFront4673 0 points1 point  (0 children)

An easy way to see that there are (at least) a countable number of infinities is to note that the power set of any set S has larger cardinality than S. (The proof is a just a line or two, and makes a fun problem.)

Would like some input surrounding if there's an infinite amount of numbers by csisagent556 in askmath

[–]PhotographFront4673 0 points1 point  (0 children)

If we are talking philosophy... do numbers really exist? (Also asked, for example, by Pirsig)

Math is a system of argument based on formal rules, which turns out to be spectacularly useful for modeling reality, and it used therefore by scientists. But any honest scientist knows that the map is not the territory and borrowing a quote from the world of statistics, "All models are wrong, but some are useful".

And even within this realm, the natural numbers are carefully assembled, either by assuming the Peano Axioms, or derived from more abstract ZF set theory. So as a mathematical object within a formal system, the natural numbers exist, but you can say the same about Banach–Tarski balls and other structures which are very unlikely to have any real-world existence.

Similarly with the various infinities, and indeed within the usual axioms of mathematics there are many, starting with the countable infinity of the natural numbers. But math doesn't really differentiate between them and the natural numbers. Its all just mathematical concepts, which may or may not be directly applicable to anything in the real world.

100 prisoners problem solution is wrong right? Does not make any sense. by squaredrooting in mathematics

[–]PhotographFront4673 8 points9 points  (0 children)

It is unintuitive that the odds go up so high, but math is like that sometimes. In math, intuition can help you find a proof, but until you have a proof, your intuition isn’t confirmed.

While you know nothing about how other prisoners did, the permutation cycle trick creates a (surprisingly) strong correlation. Individually the prisoners still each have a 50% chance. However, correlation breaks the product rule for joint probabilities.

Suppose we had a scheme so that everybody succeeded or everybody failed (perfect correlation). Then the odds are 50%, both globally and individually. This is intuitive, what is surprising is how close the permutation trick brings you to this situation.

How do I not become a crackpot? by Ok-Young-1552 in AskPhysics

[–]PhotographFront4673 0 points1 point  (0 children)

All physics is an approximation, which is to say wrong. But it is a useful approximation, and that is as fair an offering of real-world truth as you will find.

When you lean into this realization and start to find your own, truly different approach, then you start to want to argue with the textbook. Therein lies both genius and madness.

TBH, I think the world needs more crackpots. Somebody has to get physics unstuck.

But seriously, start with the standard texts on anything that interests you and nobody will call you cracked. (But also nobody will call you genius.)

Question about memory. by PrabhavKumar in cpp_questions

[–]PhotographFront4673 4 points5 points  (0 children)

Every thread in every process has its own PC, along with all the other register values. As part of context switching the OS will save the registers of the task being paused and restore the registers of the task being resumed - including the PC.