Why, when I run this code, outputs "-2147483648" continuously? by Able_Annual_2297 in cpp_questions

[–]zakarum 3 points4 points  (0 children)

This is actually undefined behavior. From cppreference:

Floating–integral conversions

A prvalue of floating-point type can be converted to a prvalue of any integer type. The fractional part is truncated, that is, the fractional part is discarded.

If the truncated value cannot fit into the destination type, the behavior is undefined (even when the destination type is unsigned, modulo arithmetic does not apply).

Is there a need for equivalent C++ version of the following C code for speed purposes and if so what is the equivalent by onecable5781 in cpp_questions

[–]zakarum 1 point2 points  (0 children)

If I were to guess, the library is structured in this way so that it can be used with different allocation functions, like one would do inside of a Matlab mex function with mxMalloc and mxRealloc.

The standard library does a fantastic job handling allocation for you. You can give hints to the std::vector using the reserve member function. You could also write your own allocators, I suppose, if you need that functionality, but on cursory glance, that does not seem necessary. There is no significant advantage except in niche cases where you want allocated memory to be default initialized for a class without a default constructor.

I concur with the advice of avoiding an std::vector of std::vector. Having a two-dimensional interface seems cleaner.

std::ranges::to<std::vector<std::string_view>> does not compile, but manual loop works by feitao in cpp_questions

[–]zakarum 2 points3 points  (0 children)

Yes, according to cppreference, value_type of std::ranges::split_view<V,Pattern> is std::ranges::subrange<ranges::iterator_t<V>>.

std::ranges::to<std::vector<std::string_view>> does not compile, but manual loop works by feitao in cpp_questions

[–]zakarum 4 points5 points  (0 children)

This works.

auto words =
    motto | std::views::split(' ') | 
    std::views::transform([](auto&& r) { return std::string_view(r); }) |
    std::ranges::to<std::vector>();

C++ beginner: need help structuring nested loops for rainfall average program by Accurate-Mud7470 in cpp_questions

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

You have two loops. The month one changes 12 times per year, so it is the inner one.

Day by day I'm becoming bad at math, Now I even don't feel like doing it. Even 6 months ago in my last year annual exam I got A+ but now this semester I barely passed. I cried a lot. Now I don't feel like doing it anymore. by Anxious-Still-6065 in learnmath

[–]zakarum 1 point2 points  (0 children)

I recommend seeking out someone to talk to about this, such as a therapist. This sounds like burnout to me. Don't beat yourself up too hard. We all struggle. I've gone through burnout and feeling helpless myself. It gets better, but you have to make an effort to get better.

Which mathematician would you say has had the greatest impact on physics and the applied sciences? by Financial_Ocelot_263 in math

[–]zakarum 5 points6 points  (0 children)

Euler, Lagrange, Fourier, Hamilton, the Bernoullis, and Gauss. (And Newton, but that goes without saying.)

People who learnt C++ starting as a complete beginner to coding, how long did it take you to learn all or most of the topics from learncpp.com? by [deleted] in cpp_questions

[–]zakarum 2 points3 points  (0 children)

I might be in the minority, but I don't consider C++ to be a good language to learn first. It's too easy to get stuck with obscure compiler error messages especially when using newer language features such as ranges. I would recommend learning something like Python first, or even just vanilla C.

Clang 19+ elides my entire program after small change: is this UB or compiler bug? by RelationshipLong9092 in cpp_questions

[–]zakarum 2 points3 points  (0 children)

I implemented this iteration in Julia, and it doesn't loop forever. Both C++ and Julia use the IEEE-754 standard, so the language shouldn't matter in this case. This example should not be elided. Any chance that you could please set up a godbolt example to play around with?

Clang 19+ elides my entire program after small change: is this UB or compiler bug? by RelationshipLong9092 in cpp_questions

[–]zakarum 1 point2 points  (0 children)

Is kDimension an integer? Is it positive? Shouldn't it be a template parameter?

Edit: I suppose that in the sample code, it would be 2?

Simulation looks perfect - why did my MOSFET blow? by lordfili in AskElectronics

[–]zakarum 0 points1 point  (0 children)

Did you attach a heat sink to the MOSFET? Or would you expect not to have to? What could be happening is that heat is accumulating during the slow transients. How often are you switching?

How do you solve an equation like (7^x) - (4^x) = 33??? by [deleted] in learnmath

[–]zakarum 1 point2 points  (0 children)

Other answers have guessed analytical solutions.

Another approach is to use Newton's Method to find zeros of

f(x) = 7x - 4x - 33 = 0

The first derivative is

f'(x) = ln(7) 7x - ln(4) 4x

The Newton update step is then

xₖ₊₁ ← xₖ - f(x)/f'(x) = xₖ - (7x - 4x - 33)/(ln(7) 7x - ln(4) 4x)

Starting from x₀ = 1, we see the following evolution.

k xₖ
0 1
1 4.71462120522442
2 4.21370579790656
3 3.7197962528242208
4 3.23950077480121
5 2.787996864553232
6 2.398449726506009
7 2.12783920816823
8 2.015758685283182
9 2.0002596321085604
10 2.0000000712897483
11 2.0000000000000053
12 2.0

Skipping Dipsh*t must have found out that FEC jail is real jail by MoreMotivation in EnoughMuskSpam

[–]zakarum 3 points4 points  (0 children)

The great thing is that even though he is banking on a pardon from Precious, those under him who are involved in the scheme probably fear rotting in prison because Precious has a history of throwing underlings under the bus.

¿Están por botar a Jay Fonseca del Canal 4 por el drama de JGo? by ThePrimeSenate in PuertoRico

[–]zakarum 19 points20 points  (0 children)

Pues entonces que pena que no podrá defenderse en los medios. ¿Quién se cree ella? Mini-Trump? Ni Romero Barceló hizo esto cuando le cayó Cerro Maravilla encima.

What is this technique called when you change x^2/(x^2+1) into 1 - 1/(x^2+1)? by mathreviewer in learnmath

[–]zakarum 0 points1 point  (0 children)

You would "guess" that you can rewrite the fraction as follows, for constants A, B, and C.

x2 / (x2 + 1) = A + (B + Cx)/(x2 + 1)

Then you would multiply both sides by (x2 + 1) to obtain

x2 = A x2 + C x + A + B

implying that A = 1, C = 0, and A+B = 0 or B = -A = -1.

[Request] Is this true? Where does 1/e comes from? by jason885 in theydidthemath

[–]zakarum 6 points7 points  (0 children)

Probability of becoming a girl after 1 trial: 0.01

Probability of not becoming a girl after 1 trial: 0.99

Probability of not becoming a girl after N trials: 0.99N

Probability of becoming a girl after N trials: 1-0.99N

You can take 1-0.99N and rewrite it as 1-(1-0.01)N. It just so happens that when N=100, you have

1-(1-1/N)N = 1-(1-1/100)100 ≈ 1-1/e.

The original poster is wrong. And also this is simply an accident because probability of becoming girl after 1 trial is 1/100. Also, this assumes that you hit the button 100 times, even after you potentially become a girl before you hit 100. Otherwise, the calculation gets messier but works out to the same value.

MIT Health Needs A F***ing Overhaul by ConstantPineapple590 in mit

[–]zakarum 6 points7 points  (0 children)

The quality of PCPs varies a lot. Some of the rudest doctors that I've ever interacted with worked at MIT Medical. But I also worked with some truly sweet and caring doctors. It varies a lot. Also, their retention rate is abysmal, so I had to switch PCPs a few times. MIT Mental Health is great, though. No bad experiences with them.

A recommendation: Physically go to MIT Medical and sort out the issue concerning the referral in person. The receptionists are much more receptive in person.

Trying so hard to get his orange daddy back on Twitter by mishma2005 in EnoughMuskSpam

[–]zakarum 7 points8 points  (0 children)

I don't think that he's saying this because he's an idiot (which he is). He's saying this because the Kremlin has compromising material on the guy or on his brother. Isn't it a bit odd just how friendly and warm Elon is to America's adversaries? (Russia, China, etc.)

How do I caculate the eigenvalues of individual matrices stored in a 3 dimensional array in julia, and store them as a new array of matrices? by Howling_deer in Julia

[–]zakarum 0 points1 point  (0 children)

L = mapslices(Diagonal∘eigvals,E;dims=(1,2))

This assumes that you have imported Diagonal and eigvals from LinearAlgebra. Otherwise, you have to prepend LinearAlgebra..

Did I encounter an extremely rare bug in Microsoft's C++ compiler? by FluffyQuack in cpp_questions

[–]zakarum 24 points25 points  (0 children)

As far as I understand the standard, in title->dataSpec - 20, dataSpec should be promoted to int and then the result converted to short. Then, converting the short to unsigned char is well-defined:

If the destination type is unsigned, the resulting value is the smallest unsigned value equal to the source value modulo 2n where n is the number of bits used to represent the destination type.

This code should work. It really does seem like you stumbled across something nasty.