Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C++ Builder > возведение числа в дробную степень


Автор: Андракула 16.1.2007, 15:48
Добрый день!

Как ловить ошибку в C++Builder 5 при возведении отрицательного числа в степень 0,5 например?

такой try-блок поймать ее не может:

Код

double f, a, b;
...
try{
f = pow(a,b);
}
catch(...){ShowMessage("----");}


а функцию Power(double a, double) (которая вроде должна работать) не находит компайлер!


такой вариант тоже на проходит:

Код

double f, a, b;
...
try{
struct _exception err;
f = pow(a,b);
_matherr(&err);
if (err.type == DOMAIN)
ShowMessage("....!");
}
}
catch(...){ShowMessage("----");}


Как быть?
Заранее всем спасибо!

Автор: segmentation_fault 16.1.2007, 16:03
Цитата(Андракула @  16.1.2007,  15:48 Найти цитируемый пост)
а функцию Power(double a, double) (которая вроде должна работать) не находит компайлер!
 
А ты Math.h подключил?

Автор: Vyacheslav 16.1.2007, 16:15
Цитата(Андракула @  16.1.2007,  15:48 Найти цитируемый пост)
такой try-блок поймать ее не может:

А где написано, что функция генерит исключение
Цитата(C++Builder Help)

Return Value

On success, pow and powl return the value calculated of x to the power of y.

Sometimes the arguments passed to these functions produce results that overflow or are incalculable. When the correct value would overflow, the functions return the value HUGE_VAL (pow) or _LHUGE_VAL (powl). Results of excessively large magnitude can cause the global variable errno to be set to

ERANGE    Result out of range

If the argument x passed to pow or powl is real and less than 0, and y is not a whole number, or if x is 0 and y is less than 0, or you call pow(0,0), the global variable errno is set to

EDOM    Domain error

Error handling for these functions can be modified through the functions _matherr and _matherrl.



Автор: Андракула 16.1.2007, 16:26
Math.h подключил

про Хелп я что то не понял, я же пытался ее с _matherr поямать, но не получилось  smile 

Автор: stmamont 16.1.2007, 16:28
Подключать надо Math.hpp для работы с Power по-моему

Автор: Vyacheslav 16.1.2007, 16:29
Код

#include <math.h>
#include <iostream>
#include <errno.h>

//---------------------------------------------------------------------------

#pragma argsused
int main(int argc, char* argv[])
{
  double f = pow(-100,0.5);
  
  if (errno  == EDOM)
       std::cout << "Error" << std::endl;


    return 0;
}


Автор: Андракула 16.1.2007, 16:33
Да с #include <math.hpp> все работает! smile 

Большое спасибо!!!

Автор: Vyacheslav 16.1.2007, 16:51
Цитата(Андракула @  16.1.2007,  16:26 Найти цитируемый пост)
про Хелп я что то не понял, я же пытался ее с _matherr поямать, но не получилось    

_matherr служит для изменения дефолтного поведения. Если хотите, что бы pow генерил исключение, то надо переопределить _matherr
Код

#include <math.h>
#include <string.h>
#include <exception>
#include <string>
#include <iostream>

int _matherr (struct _exception *a)
{
  throw std::domain_error("pow: DOMEN error");
  return 0;
}

#pragma argsused
int main(int argc, char* argv[])
{
  try {
    double f = pow(-100,0.5);
  }
  catch(std::exception& exp)
  {
      std::cout << exp.what() << std::endl;
  }
    return 0;
}


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)