Saturday, March 30, 2019

c++ - comparing float variable

When comparing floats, you have to compare them for being "close" instead of "equal." There are multiple ways to define "close" based on what you need. However, a typical approach could be something like:


namespace FloatCmp {
const float Eps = 1e-6f;
bool eq(float a, float b, float eps = Eps) {
return fabs(a - b) < eps;
}
//etc. for neq, lt, gt, ...
}

Then, use FloatCmp::eq() instead of == to compare floats.

What is a "static" function in C?



The question was about plain functions, not static methods, as clarified in comments.



I understand what a static variable is, but what is a static function?



And why is it that if I declare a function, let's say void print_matrix, in let's say a.c (WITHOUT a.h) and include "a.c" - I get "print_matrix@@....) already defined in a.obj", BUT if I declare it as static void print_matrix then it compiles?




UPDATE Just to clear things up - I know that including .c is bad, as many of you pointed out. I just do it to temporarily clear space in main.c until I have a better idea of how to group all those functions into proper .h and .c files. Just a temporary, quick solution.


Answer



static functions are functions that are only visible to other functions in the same file (more precisely the same translation unit).



EDIT: For those who thought, that the author of the questions meant a 'class method': As the question is tagged C he means a plain old C function. For (C++/Java/...) class methods, static means that this method can be called on the class itself, no instance of that class necessary.


Friday, March 29, 2019

character - What nationality is Bane? - Movies & TV



It has been clearly established that Ra's Al Ghul & Talia Al Ghul are Moroccan. And it is assumed that The Pit is in Morocco.



Does this mean that we should assume Bane is also Moroccan? Or is it just coincidence that he was in a Moroccan prison and involved with two Moroccans?


Answer




An article written by NBC has the following quote.




DC Comics describes Bane’s father as a British mercenary and his mother a rebel from the Caribbean. His life spent in a jail on the fictional Caribbean Island of Santa Prisca, a seemingly Spanish name.




So basically Bane is half British, half Caribbean which would explain why he is so tanned yet still speaks perfect English.



Link to the article here


c++ comparison of two double values not working properly




Look at this code:



#include 
#include
using namespace std;
class Sphere
{

double r;
public:
double V() const { return (4/3) * 3.14 * pow(r,3); }
bool equal(const Sphere& s) const
{
cout << V() << " == " << s.V() << " : " << ( V() == s.V() );
return ( V() == s.V() );

}


explicit Sphere(double rr = 1): r(rr){}

};
main()
{
Sphere s(3);
s.equal(s);
}



The output is 84.78 == 84.78 : 0 which means the same method doesn't return the same value every time, even though all parameters are static?



But if I write 3.0 instead of 3.14 in the V() method definition, like this:



double V() const { return (4/3) * 3.0 * pow(r,3); }


Then, the output is: 84.78 == 84.78 : 1



What is going on here? I need this method, for my program, which will compare volumes of two objects, but it is impossible? I banged my head for so long to figure out what is the cause of the problem and luckily I found it, but now I don't understand why?? Does it have something to do with the compiler (GCC) or am I missing something important here?



Answer



Comparing floating point values using the == operator is very error prone; two values that should be equal may not be due to arithmetic rounding errors. The common way to compare these is to use an epsilon:



bool double_equals(double a, double b, double epsilon = 0.001)
{
return std::abs(a - b) < epsilon;
}

Cast one dynamic to the type of another in c#

I'm trying to write a generic function that compares expected results from reflection (but where the expectation is provided in configuration by users rather than at design time) with the actual results for arbitrary properties.



I'm running into an issue where the expected type doesn't always reflect the returned type by default - e.g. my reflection result (in a dynamic) may be an int, where the expected result is an enum member (inheriting from int).



I'd like, therefore to do the following:



if ((dCurrentValue as typeof(this.CheckValue)) != this.CheckValue) { oOut = false; }



however, this doesn't seem to work. From fumbling around the web, I've managed to find that either System.Activator or Convert.ChangeType() may be my friends. However, so far they're not working as I'd expect - e.g.:



dCurrentValue = Convert.ChangeType(dCurrentValue, this.CheckValue.GetType());


throws an exception (for the pair that alerted me to the issue) that Invalid cast from 'System.Int32' to 'Microsoft.Office.Core.MsoTriState' - which I know to be wrong, since:



(int)Microsoft.Office.Core.MsoTriState.msoTrue == -1                                    // true
((Microsoft.Office.Core.MsoTriState)(-1)) == Microsoft.Office.Core.MsoTriState.msoTrue // true



NB that whilst I could put a shim in to solve for MsoTriState (i.e. check type of this.CheckValue, and explicit cast if applicable), I'd rather do this in a way that'll work for unknown enum entries.



EDIT: Thanks to the comments below, I've added a test before my tests of the form:



if (((Type) this.CheckValue.GetType()).IsEnum)
{
dCurrentValue = Enum.Parse(this.CheckValue.GetType(), dCurrentValue.ToString());
}



which fixes my immediate issue. My guess is this combined with Convert.ChangeType() (which as I've mentioned, doesn't seem to like converting Enums to Ints) will cover most situations.

c++ - double or float comparison

I've seen posts like:



What is the most effective way for float and double comparison?



Compare two floats



And many other related posts.



I saw in d3js library, it uses the following comparison:




  return a < b ? -1 : a > b ? 1 : a >= b ? 0 : NaN;


Is it OK to use this in C/C++ to do the comparison of double and float?

error handling - PHP - failed to open stream: no such host is known



Hi i have a issue in simple html dom code it show this error :-




file_get_contents(http://www.arakne-links.com)
[function.file-get-contents]: failed to open stream:

php_network_getaddresses: getaddrinfo failed: No such host is known.
in D:\xampp\htdocs\scrap\simple_html_dom.php on line 75




because this url http://www.arakne-links.c is not working now i want



to know is there any way to skip the url which is not working..



here is the code which i am using




ini_set('display_errors', 'on'); 
include_once('../../simple_html_dom.php');

// create HTML DOM

$htmls = file_get_html('http://info.vilesilencer.com/top');
foreach($htmls->find('a[rel="nofollow"]') as $e):
$test = $e->href;
$url = array( $test );
$html = array();

foreach( $url as $key=>$value ) {

// get html plain-text for webpage & assign to html array.

$html = file_get_html( trim($value) );

// echo html plain text:
echo $html->find('title', 0)->innertext;

}

endforeach;


Please Help me to fix this issue.



Thankyou


Answer



How about checking the URL before parsing?



ini_set('display_errors', 'on'); 

include_once('simple_html_dom.php');

function urlOk($url) {
$headers = @get_headers($url);
if($headers[0] == 'HTTP/1.1 200 OK') return true;
else return false;
}

// create HTML DOM


$htmls = file_get_html('http://info.vilesilencer.com/top');
foreach($htmls->find('a[rel="nofollow"]') as $e):
$test = $e->href;
$url = array( $test );
$html = array();
foreach( $url as $key=>$value ) {
// get html plain-text for webpage & assign to html array.
if (urlOk(trim($value))) {
$html = file_get_html( trim($value) );
echo $html->find('title', 0)->innertext;

echo "
";
} else {
echo 'Error: URL '.$value.' doesn\'t exist.
';
}
}
endforeach;
?>

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...