23,265 questions
0
votes
3
answers
96
views
If I subclass int, how to make arithmetic methods return the derived type?
I want to construct objects which support arithmetic operations and have extra methods, e.g:
class Timestamp(int):
def as_seconds(self):
return self / 90000
The extra method works:
>&...
4
votes
1
answer
104
views
How to evaluate arithmetic expression in pseudo‑SQL style (Lua 5.1)
In love2D I have a string that represents a mathematical expression, for example:
local expr1 = "[p35] div [p36]"
local expr2 = "((([p35]*100) div [p36]) mod 100)"
local params = {...
-1
votes
1
answer
115
views
Integer or Double vector? [duplicate]
print(krit)
names weight
1 may 36
2 mayer 49
3 mayo 35
4 mali 50
> mean(krit$weight)
[1] 42.5
> typeof(weight)
[1] "double"
> typeof(names)
[1] "character&...
Advice
1
vote
21
replies
401
views
Is there a reason why signed integer is used for the ssize_t?
From the man pages, I found that size_t has the range of 0 to SIZE_MAX, and ssize_t has the range of -1 to SSIZE_MAX. So, after printing those values on a 64bit system, I have the following results:
...
1
vote
2
answers
215
views
How to set int array pointer within for loop in c
The following code is trying to set the test array in the first for loop, but it can't be used in the second for loop.
int main()
{
int *test[3];
/** Try to set the array value one by ...
2
votes
1
answer
116
views
In python is there a way to get len with int data type and not and index-sized integer? [duplicate]
I working on a code where it is necessary to have large integers, here is a simplified version of it (len_class.py):
class BigLen():
def __len__(self):
return 10**100
However I'm running ...
4
votes
2
answers
180
views
How can I convert a char into an int within the range of 0 and 255 in C++
I am trying to make my own file compressor and some of the chars in the file that I am trying to compress are '�'.
I tried:
#include <iostream>
int main(){
std :: cout << (int)'�';
...
3
votes
2
answers
234
views
C treating variable declared to be a double as an int
I am trying to solve a problem posed in this question which asks that a program be written that calculates logarithms of numbers without including <math.h>.
I wrote the following program:
#...
4
votes
2
answers
190
views
integer comparision; difference in behavior between Clang and GCC 12
I'm seeing a strange (to me) difference in behavior between Clang and GCC when comparing an integer with its negation. Also, pre-v12 GCC behaves like Clang.
Code is below, but also here's a live link ...
3
votes
1
answer
106
views
How does Rust lexer handle the integer literal?
The following lex rules are copied from int_literal in docs of rust nightly.
INTEGER_LITERAL ->
( DEC_LITERAL | BIN_LITERAL | OCT_LITERAL | HEX_LITERAL ) SUFFIX_NO_E?
DEC_LITERAL -> ...
5
votes
1
answer
136
views
Conversion to int with Unicode strings
I recognized that int(unicode_string) sometimes gives obscure results.
E.g. int('᪐᭒') == 2.
>>> bytes('᪐᭒', 'utf-8')
b'\xe1\xaa\x90\xe1\xad\x92'
>>> [f'U+{ord(c):04X}' for c in '᪐᭒']
...
-4
votes
2
answers
126
views
In this python script that checks for integer values, why is one entry not removed? It's the letter x. Works for every other line [closed]
This script takes lines from a file of lottery numbers, and is supposed to check for errors. For example, in the following:
week 1;17,19,35,23,8,20,36
week 2;24,28,35,8,3,22
week x;23,29,38,1,35,18,25
...
0
votes
0
answers
62
views
Sum of integer data and numeric data is unexpected numeric value, R [duplicate]
Why is the sum of the iaea_mass_dat_trunc$AM_WN (integer) and iaea_mass_dat_trunc$AM_frac (numeric) 499496.6 for all results? How do I get the appropriate result (for example, the sum of the values in ...
5
votes
5
answers
422
views
Why Zero has no decimal integer spelling in C?
I am reading modern C, and on page 66, I come across following section:
Remember that value 0 is important. It is so important that it has a lot of equivalent
spellings: 0, 0x0, and ’\0’ are all the ...
1
vote
1
answer
145
views
How does typecasting between different sized integers work in C++?
Imagine: int full = static_cast<int>(uint8_t_var);
Does anything actually happen under the hood here? If you're on a machine with 64 bit registers, I assume that the higher bits of uint8_t_var ...
2
votes
2
answers
313
views
How to get integer unix timestamps in python? Not casting float to integer
The float timestamp used in Python seems to be problematic. First, it will lose its precision as time goes on, and second, the timestamp is not usable, for example, storing in a database big integer ...
0
votes
1
answer
35
views
How should I perform an elementwise cast of an OpenCL C vector value?
OpenCL C supports "vector data types" - a fixed number of scalar types which may be operated on together, as though they were a single scalar, mostly: we can apply arithmetic and logic ...
2
votes
2
answers
127
views
Convert String input to int array in Java
How do I convert a numeric string input from a user into an integer array in Java?
What I did so far is:
# Get User input
System.out.println("Guess the number: ");
String input = read....
1
vote
1
answer
62
views
Numpy array from the image is not squaring right
I have this program that is supposed to select one color channel from an image, and square each element elementwise. However, it is not returning any results greater than the values in the first array?...
1
vote
1
answer
98
views
Separate the integer part from the decimal part of a real number entered from the keyboard in Java
I have this code that separates the integer part from the decimal part
import java.io.*;
import java.util.Scanner;
public class hoja1ej9
{
public static void main(String[] args)
{
...
-4
votes
6
answers
215
views
Text.text = int + X?
I'm trying to make my own video game in Unity. I have a series of biomes.
They eventually evolve from bad to good, based on a float converted into an int going from 0 to 100, giving a biome Value of +...
0
votes
1
answer
98
views
Why does int in my DP array cause overflow in Coin Change II problem?
class Solution {
public:
int change(int amount, vector<int>& coins) {
vector<unsigned int > dp(amount+1, 0);
dp[0]=1;
for (int value: coins){
...
6
votes
1
answer
141
views
Rules on the choice of type for a literal whose value is too big
I was testing the behaviour of the big three (GCC, clang, MSVC).
I'm not sure what is standard-compliant.
Consider this code:
// p43.cxx
#include <type_traits>
constinit auto n1 = ...
1
vote
1
answer
59
views
Android Kotlin: Function toIntOrNull() not working as expected
Before issuing the ID ( took from a String list ) to the argument of a function call, I first check if it represents a number:
if ( retVal.isValidInt() )
{
fetchProducts(
...
3
votes
2
answers
149
views
Finding the Least Common Multiplier to Turn a Vector of Numbers Into Integers
I have the following vector.
Vector_1 <- c(0.1, 0.9, 0.5, (1 / 3), 0.5344)
I want to multiply each element of this vector by the same number such that each number in the resulting vector is an ...
-1
votes
1
answer
207
views
How should I convert a float to an unsigned integer? [closed]
I need to convert floats to unsigned integers for a project I'm working on — similar to the basic float-to-int cast, but with an unsigned int as output, returning zero for floats that are negative. I ...
2
votes
3
answers
120
views
Confused by difference between expression inside if and expression outside if
Context:
I want to verify the fact that under 32-bits, Ox8000 0000 - 1 = Ox7FFF FFFF, so if both of them are interpreted as signed integers, the sign will change from negative to positive.
Here goes ...
-1
votes
2
answers
150
views
Convert ASCII string to integer for registry key (WmiMonitorID.UserFriendlyName)
im trying to get a powershell script to query display EDID info and put it into a reg key. when trying to add the key, it says its not found, but the reg key tree exists. it turns out that all the ...
3
votes
2
answers
160
views
How does PyPy implement integers?
CPython implements arbitrary-precision integers as PyLongObject, which extends PyObject. On 64-bit platforms they take at least 28 bytes, which is quite memory intensive. Also well-known is that it ...
-2
votes
3
answers
285
views
From 1 to n, count how many integers there are with the sum of divisors being a prime number? [closed]
Let there be integer n.
Count how many numbers there are from 1 to n that has the sum of divisors being a prime number.
Example:
Input: 10
Output: 3
Explanation: There are 3 numbers with the sum of ...
-4
votes
1
answer
131
views
Can you explain how int function(int()) works in Python?
Hello I just have a question about int function. It convert data(for example, string) to int data.
My question is how this function works like this? I tried to implement int function myself without ...
1
vote
1
answer
115
views
Taking too long to run code in loop in C when user input is int instead of float
#include <stdio.h>
int main()
{
int i, count;
printf("Enter an integer: ");
scanf("%d", &count);
for (i = 1; i <= count; i++)
{
printf(&...
1
vote
3
answers
209
views
C int overflow strange behavior
I know that signed int overflow is undefined in C.
I understood this as the resulting number is not predictable, but it is still a number.
However, this code does not behave like the result of an ...
0
votes
0
answers
49
views
C# when deploid as nuget package a simple calculation gives wrong results
The following function
public int GetNumber()
{
int num = (condition1 ? 3 : (condition2 ? 1 : 2));
int num2 = (condition3 ? 6 : (condition4 ? 3 : 0));
return num + num2;
}
returns the ...
1
vote
2
answers
118
views
Different representation for signed integer on the same platform
Is there something in the C standard that implies only one representation for signed integers should be used? Specifically, does the standard prohibit using, for example, one's complement for int ...
-7
votes
3
answers
93
views
How do you get numbers to match even if they are not in the correct order? [closed]
I think my question is a little confusing, so here is what I am trying to do:
I would like to take my inputted number, 683, and I would like my program to compare it with this given number, 836. I am ...
-1
votes
1
answer
57
views
Python or statements: program ignores the second part of the or statement if the first part invalid? [duplicate]
I have an if statement where a variable smallest is being compared to a variable num (integer). That if statement is in a loop that goes through an array of integers. smallest starts out as None, my ...
1
vote
0
answers
75
views
Numpy ints convert into python ints within numpy array
I'm writing code to group points around each point in a curve into concentric regions. Then I find the geometric median of each region as way of determining its "shape".
The point indices ...
4
votes
2
answers
268
views
Is a > c / b a safer equivalent (but avoiding overflow) version of a * b > c for positive integer division?
This came up in a C++ program I am writing for iterating over a loop, where it was pointed out that the for loop condition a * b > c could overflow. I am rusty with integer math but I think, ...
0
votes
1
answer
29
views
Why this Fib() implementation on LLVM-IR can't output correctly from Fib(93)?
Here is my implementation of fib on LLVM-IR, everything work fine under i64 limit, but above that, it can't printf the correct result, even with attempt to truncate i128 into 2 halves of i64 :
define ...
0
votes
0
answers
85
views
I don't know how to get Integers from another function without calling the variables that hold said number
I am taking Google's Kotlin Basics course for Android development. I am now at a exercise that wants me to create a function that calculates the sum of whatever number two variables hold (there are ...
4
votes
1
answer
270
views
Most efficient way to wrap 32 bit integer to 16 bit value?
I am writing some DSP code that performs wavefolding distortion on an input signal. This code applies amplitude gain (multiplies input by a gain value) then wave folds the input such that the final ...
0
votes
2
answers
137
views
Why an integer has different value even when its position is not changed
I was trying to do one coding questions solve, that says:
Write an alternate version of squeeze(s1, s2) which deletes each character in s1 which matches any character in the string s2.".
Well i ...
3
votes
3
answers
169
views
Why do a string and integer with identical memory values print differently?
I'm making my way through Learn C the Hard Way, in which the author often asks students to intentionally "break" the examples to learn debugging.
In Exercise 11, it's suggested to attempt to ...
-1
votes
1
answer
77
views
How can I convert a whitespace separated string to integers in rust
I'm trying to convert a string example "12 34 56" into a vector of integers using Rust. I tried using .chunks() and a few other things the code I used works as expected for "123456"...
1
vote
1
answer
97
views
Why is an empty integer array filled with seemingly garbage data in C? [duplicate]
I'm currently learning C while following along with The C Book and one of the excercises is to write a program that lets the user input a number, which the program will spit back afterwards.
While ...
3
votes
2
answers
227
views
How do I get a larger integer than the maximum bitint can provide?
I'm trying to make a simple Fibonacci calculator, but bitints max bit number of 65535 is too small.
Is there any way to get a bigger integer?
typedef unsigned _BitInt(65535) u65535;
int main(void) {
...
0
votes
1
answer
54
views
In .NET will a `decimal` equal to an `int` always cast to that `int`
In .NET, assuming that a decimal is not so large that it cannot be represented (or cause an overflow), will a decimal that is equal to an int always cast to the int that it is equal to ?
Same question ...
0
votes
2
answers
76
views
Recasting values in Pandas dataframe that meet specific requirements
I have a Pandas dataframe that has been created by importing from an Excel spreadsheet. When using the .dtypes method, the column appears to be of data type object. However, within the single column, ...
1
vote
1
answer
117
views
What is the difference between using (double) versus multiplying by 1.0 when type casting an integer to a double?
I was wondering what the difference is between converting an integer to a double using double versus multiplying the integer by 1.0
int a = 4
int b = 3
double c = 1.0 * a / b
double d = (double) a / ...