当前位置: 首页>>代码示例>>C++>>正文


C++ Fixnum::neg方法代码示例

本文整理汇总了C++中Fixnum::neg方法的典型用法代码示例。如果您正苦于以下问题:C++ Fixnum::neg方法的具体用法?C++ Fixnum::neg怎么用?C++ Fixnum::neg使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Fixnum的用法示例。


在下文中一共展示了Fixnum::neg方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: test_neg

  void test_neg() {
    Fixnum* min = Fixnum::from(FIXNUM_MIN);
    Integer* b = min->neg(state);

    TS_ASSERT(kind_of<Bignum>(b));
    TS_ASSERT_EQUALS(FIXNUM_MAX + 1, b->to_native());

    Fixnum* max = Fixnum::from(FIXNUM_MAX);
    b = max->neg(state);

    TS_ASSERT(kind_of<Fixnum>(b));
    TS_ASSERT_EQUALS(FIXNUM_MIN + 1, b->to_native());

    TS_ASSERT_EQUALS(Fixnum::from(3)->neg(state),  Fixnum::from(-3));
    TS_ASSERT_EQUALS(Fixnum::from(-3)->neg(state),  Fixnum::from(3));
  }
开发者ID:chuckremes,项目名称:rubinius,代码行数:16,代码来源:test_fixnum.hpp

示例2: from_cstr

  Integer* Integer::from_cstr(STATE, const char* str, const char* end,
                              int base, Object* strict)
  {
    if(base == 1 || base > 36) return nil<Integer>();

    // Skip any combination of leading whitespace and underscores.  Leading
    // whitespace is OK in strict mode, but underscores are not.
    while(isspace(*str) || *str == '_') {
      if(*str == '_') {
        if(CBOOL(strict)) {
          return nil<Integer>();
        } else {
          return Fixnum::from(0);
        }
      }

      str++;
    }

    bool negative = false;

    if(*str == '-') {
      str++;
      negative = true;
    } else if(*str == '+') {
      str++;
    }

    int detected_base = 0;
    const char* str_start = str;

    /* Try to detect a base prefix. We have to do this even though we might
     * have been told the base, we have to know if we should discard the bytes
     * that make up the prefix if it's redundant with the passed in base.
     *
     * For example, if base == 16 and str == "0xa", we return 10.  But if base
     * == 10 and str == "0xa", we fail because we rewind and try to process 0x
     * as part of the base 10 string.
     */
    if(*str == '0') {
      str++;
      switch(*str++) {
      case 'b': case 'B':
        detected_base = 2;
        break;
      case 'o': case 'O':
        detected_base = 8;
        break;
      case 'd': case 'D':
        detected_base = 10;
        break;
      case 'x': case 'X':
        detected_base = 16;
        break;
      default:
        // If passed "017" and a base of 0, that is octal 15.  Otherwise, it
        // is whatever those digits would be in the specified base.
        str--;
        detected_base = 8;
      }
    }

    // If base is less than 0, then it's just a hint for how to process it
    // if there is no base detected.
    if(base < 0) {
      if(detected_base == 0) {
        // Ok, no detected because, use the base hint and start over.
        base = -base;
        str = str_start;
      } else {
        base = detected_base;
      }

    // If 0 was passed in as the base, we use the detected base.
    } else if(base == 0) {

      // Default to 10 if there is no input and no detected base.
      if(detected_base == 0) {
        base = 10;
        str = str_start;

      } else {
        base = detected_base;
      }

    // If the passed in base and the detected base contradict each other, then
    // rewind and process the whole string as digits of the passed in base.
    } else if(base != detected_base) {
      // Rewind the stream and try and consume the prefix as digits in the
      // number.
      str = str_start;
    }

    int max_digits = DIGIT_BIT / digit_bits[base];
    int count = 0;

    int digit = 0;
    mp_digit shift = base, value = 0;
    mp_int a = { 0, 0, 0, 0, 0, };

//.........这里部分代码省略.........
开发者ID:JesseChavez,项目名称:rubinius,代码行数:101,代码来源:integer.cpp


注:本文中的Fixnum::neg方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。