integer

Reliable integer typecasting

与世无争的帅哥 提交于 2020-01-16 01:06:13
问题 I'm working on a data validator package for PHP (mostly as an exercise in some of the new additions in 5.3 such as namespaces). I'm planning to have a main validator class which uses plugin classes to do the actual validation. The first validation plugin I'm building is just a simple one for integers. The source is below: namespace validator\validatorplugins; class Integer implements ValidatorPlugin { private $field = NULL; public function cast () { return ($this -> field = (int) ($this ->

Doctrine2 column type integer of length 255 doesn't apply to DB

被刻印的时光 ゝ 提交于 2020-01-15 12:36:05
问题 I'm using Doctrine 2 for my DB and I'm currently trying to set a length for one of my column that has an "integer" type but it just doesn't seem to be taken. /** @Column(type="integer", length=255) */ protected $number; When I check my database it still says "int(11)" instead of "int(255)". I haven't found anyone experiencing that problem on google or maybe my keywords ain't working so good. I've seen a few example of people using length=XXX and not complaining about it so I thought it would

strange behavior when fixing integer lengths in bash

旧街凉风 提交于 2020-01-15 10:13:25
问题 In my bash script, I set-up the following operations year=0050 echo $(printf %04d $year) >0040 I do not understand why 0040 is returned instead of 0050. I eventually found that to get the system to print 0050 correctly, I will have to do this instead. year=50 echo $(printf %04d $year) >0050 Are there any insights as to why the first case happens? 回答1: It's because numbers with a leading zero are interpreted as octal by Bash, and octal 50 is decimal 40. To fix it, you can either strip the

Python sum() returns negative value because the sum is too large for 32bit integer

人走茶凉 提交于 2020-01-14 14:47:31
问题 x = [1, 2, 3, ... ] y = sum(x) The sum of x is 2165496761, which is larger than the limit of 32bit integer So sum(x) returns -2129470535. How can I get the correct value by converting it to long integer? Here is my import list: import math, csv, sys, re, time, datetime, pickle, os, gzip from numpy import * 回答1: The reason why you get this invalid value is that you're using np.sum on a int32 . Nothing prevents you from not using a np.int32 but a np.int64 or np.int128 dtype to represent your

How does python represent such large integers?

血红的双手。 提交于 2020-01-14 12:46:29
问题 In C, C++, and Java, an integer has a certain range. One thing I realized in Python is that I can calculate really large integers such as pow(2, 100) . The same equivalent code, in C, pow(2, 100) would clearly cause an overflow since in 32-bit architecture, the unsigned integer type ranges from 0 to 2^32-1. How is it possible for Python to calculate these large numbers? 回答1: Basically, big numbers in Python are stored in arrays of 'digits'. That's quoted, right, because each 'digit' could

c++ convert a fractional part of a number into integer

微笑、不失礼 提交于 2020-01-14 08:51:51
问题 I needed to convert a fractional part of a number into integer without a comma, for example I have 3.35 I want to get just 35 part without zero or a comma, Because I used the modf() function to extract the the fractional part but it gives me a 0.35 if there is any way to do that or to filter the '0.' part I will be very grateful if you show me how with the smaller code possible, 回答1: A bit more efficient than converting to a string and back again: int fractional_part_as_int(double number, int

c++ convert a fractional part of a number into integer

纵饮孤独 提交于 2020-01-14 08:51:07
问题 I needed to convert a fractional part of a number into integer without a comma, for example I have 3.35 I want to get just 35 part without zero or a comma, Because I used the modf() function to extract the the fractional part but it gives me a 0.35 if there is any way to do that or to filter the '0.' part I will be very grateful if you show me how with the smaller code possible, 回答1: A bit more efficient than converting to a string and back again: int fractional_part_as_int(double number, int

MATLAB genetic algorithm optimization returns integer values higher than boundaries and violates inequality constraints. Why?

天大地大妈咪最大 提交于 2020-01-14 06:13:27
问题 I'm using MATLAB R2016a genetic algorithm optimization toolbox to optimize 80 integer values. I have these constraints: x(80) > x(79) > x(78) > x(77) > x(76) ... x(5) > x(4) > x(3) > x(2) > x(1) The range for all integer variables is between 1 and 500. I used this code in MATLAB: f = @(x)Cost_function(x, my_data); num_of_var = 80; for mx = 1:num_of_var-1 A(mx,:) = [zeros(1,mx-1),1,-1, zeros(1,num_of_var-mx-1)]; end b = repmat(-3, [num_of_var-1,1]); lb = ones([num_of_var-1,1]); up = repmat(500

Minimum number of bytes that can contain an integer value

爱⌒轻易说出口 提交于 2020-01-14 05:24:05
问题 Given an integer value, I need some way to find out the minimum number of bytes needed to store the value. The value may be signed or unsigned, up to 64-bit . Also take the sign bit into account for signed integers. For example: 8 requires 1 byte at minimum unsigned 255 requires 1 byte at minimum signed 255 requires 2 bytes at minimum 4351 requires 2 bytes at minimum -4294967296 requires 5 bytes at minimum unsigned 0xFFFFFFFFFFFFFFFF requires 8 bytes at minimum I can think of a quick-and

Most efficient portable overflow detection? [duplicate]

泄露秘密 提交于 2020-01-13 08:50:06
问题 This question already has answers here : Catch and compute overflow during multiplication of two large integers (12 answers) Closed 5 months ago . In close to the metal languages like C, C++ and D, what's the most efficient reasonably portable way (i.e. w/o using assembler, though you may assume two's complement arithmetic and wrap-around behavior) to detect overflow of an unsigned 64-bit integer on multiplication? 回答1: You can detect overflow in advance by dividing the maximum value