Artöm
Write it yourself
rex
i cant use strrev function because it is not defined in gcc compiler
rex
and again if i reversing the string character wise just tell me logic
Artöm
Use for, swap i and (last-i) chars, i from 0 to (length/2)
Artöm
C or C++?
Artöm
i cant use strrev function because it is not defined in gcc compiler
You may try adding -D_GNU_SOURCE to compiler invocation
Pavel
I want to implement a simple float comparison with epsilon in my project. But I also want to check with assertions that the values have comparable magnitudes with the given epsilon (so no one tries to compare one billion with something else using epsilon of 0.0001f). Is there a way to achieve that? bool AreFloatsComparable(float a, float b, float epsilon) { return ??? } bool AreEqualWithEpsilon(float a, float b, float epsilon) { Assert(AreFloatsComparable(a, b, epsilon), "error: ..."); return fabs(a - b) < epsilon; }
Mar!o
[[nodiscard]] inline NO_ALIAS auto is_zero(const f32 v) noexcept->bool { return abs(v) < 1e-6f; } [[nodiscard]] inline NO_ALIAS auto is_one(const f32 v) noexcept->bool { return is_zero(v - 1.f); } [[nodiscard]] inline NO_ALIAS auto near_equal(const f32 a, const f32 b) noexcept->bool { if (is_zero(a - b)) return true; const s32 a2 = *reinterpret_cast<const s32*>(&a); const s32 b2 = *reinterpret_cast<const s32*>(&a); if (a2 < 0 != b2 < 0) return false; return abs(a2 - b2) <= 0b0100; } [[nodiscard]] constexpr NO_ALIAS auto within_epsilon(const f32 a, const f32 b, const f32 epsilon) noexcept->bool { const f32 num = a - b; return -epsilon <= num && num <= epsilon; }
Mar!o
Here just replace f32 with float and s32 with int, NO_ALIAS with __declspec(noalias) (only if you are on windows, otherwise remove it).
Mar!o
This is code for correctly comparing floating point numbers using epsilons :)
Mar!o
Your welcome, just write if you need more help :)
Mar!o
Remember it is C++17 if you need it for C or an older std just write too
Mar!o
Alright :)
Álvaro
Hi everybody
Anonymous
Álvaro
Just entering this awesome world of C in 42Madrid
Álvaro
Hope learn a lot from you
klimi
Hope learn a lot from you
most things are up to you tho. You are the person who is so awesome to learn all the things
Álvaro
😊👍
Lauri
dayan: What getting memory location of variable can be used for Useful for
klimi
um changing the variable or reading it? (im no pro)
Lauri
No
klimi
okay.
MilkBeforeCereal
No
but essentially it is used for that? 🤔
Lauri
klimi
yes?
Lauri
What can I do with that memory adress
Mar!o
It is useful in many cases for example function parameters: A pointer is like an int, but it contains a memory adress. If you want to pass a big object to a function, it would copy the object from the callsite. If you pass a pointer it only copies the pointer and you can access the big data using the pointer
Mar!o
Which is of course much faster
桃桃乌龙
Learn yourself some assembly language and you’ll find out that pointers is pretty much everywhere.
Pavel
[[nodiscard]] inline NO_ALIAS auto is_zero(const f32 v) noexcept->bool { return abs(v) < 1e-6f; } [[nodiscard]] inline NO_ALIAS auto is_one(const f32 v) noexcept->bool { return is_zero(v - 1.f); } [[nodiscard]] inline NO_ALIAS auto near_equal(const f32 a, const f32 b) noexcept->bool { if (is_zero(a - b)) return true; const s32 a2 = *reinterpret_cast<const s32*>(&a); const s32 b2 = *reinterpret_cast<const s32*>(&a); if (a2 < 0 != b2 < 0) return false; return abs(a2 - b2) <= 0b0100; } [[nodiscard]] constexpr NO_ALIAS auto within_epsilon(const f32 a, const f32 b, const f32 epsilon) noexcept->bool { const f32 num = a - b; return -epsilon <= num && num <= epsilon; }
By the way, there seems to be a UB (at least in general case, where strict aliasing is enabled). Type punning between these values is violating the rules of strict aliasing. See the last code example in the Notes section https://en.cppreference.com/w/cpp/language/reinterpret_cast These lines const s32 a2 = *reinterpret_cast<const s32*>(&a); const s32 b2 = *reinterpret_cast<const s32*>(&a); can be replaced with these for example s32 a2, b2; memcpy(&a2, &a, sizeof(f32)); memcpy(&b2, &b, sizeof(f32)); More detailed explanation https://gist.github.com/shafik/848ae25ee209f698763cffee272a58f8
klimi
@bastyav you alright dude?
Pavel
You are right the memcpy solution is better but the old one shouldn't be a ub because a2 and b2 are copies of the dereferenced punned pointer which are not affected by the address meaning that there is no aliasing second pointer to them. Gonna read the post tough :)
Probably, but the compiler is free to do any wierd stuff in this case, so I would be at least aware of this BTW, I forgot to include one of the links (updated my message now) https://en.cppreference.com/w/cpp/language/reinterpret_cast
Idiriz906
I am human
Idiriz906
😭😭😭why the admins restricted me to write here-!!!!
Idiriz906
Would not i get here benifits from the experiers 😭😭
Naveen
Hi
klimi
Hi
Hi
Naveen
Hi
Hello
klimi
Nameful
hi
Hi
Naveen
hi
How are u and may i know from where are talking
MilkBeforeCereal
lul
klimi
How are u and may i know from where are talking
i am talking from my chair and i am well thanks for asking
kenny3fcb
lol
klimi
Naveen
Thanks
Place pucha
MilkBeforeCereal
?
"asked for location"
Dima
lol
MilkBeforeCereal
lulmao
MilkBeforeCereal
klimi
in what language
MilkBeforeCereal
Hindi
klimi
Place pucha
My chair in my house
klimi
where i live
Naveen
where i live
So where do u live
klimi
So where do u live
You are persistent aren't ya
I_Interface
#ot
Zel
Hindi? South Asia probably India.
MilkBeforeCereal
What u think
what's the point in asking where someone lives on the internet? They could say literally anything
MilkBeforeCereal
Hindi? South Asia probably India.
yeah that's an Indian name
MilkBeforeCereal
I am from Saturn
I'm from Uranus daddy
MilkBeforeCereal
wait no
Zel
lewd
rex
i want to copy the lines whose size is >20