long-integer

Wrong result by Java Math.pow

≯℡__Kan透↙ 提交于 2019-11-27 09:40:53
If you try to run the following code public class Main { public static void main(String[] args) { long a = (long)Math.pow(13, 15); System.out.println(a + " " + a%13); } } You will get "51185893014090752 8" The correct value of 13^15 is 51185893014090757 , i.e. greater than the result returned by Math.pow by 5 . Any ideas of what may cause it? You've exceeded the number of significant digits available (~15 to 16) in double-precision floating-point values. Once you do that, you can't expect the least significant digit(s) of your result to actually be meaningful/precise. If you need arbitrarily

C: Casting minimum 32-bit integer (-2147483648) to float gives positive number (2147483648.0)

不打扰是莪最后的温柔 提交于 2019-11-27 09:14:29
I was working on an embedded project when I ran into something which I thought was strange behaviour. I managed to reproduce it on codepad (see below) to confirm, but don't have any other C compilers on my machine to try it on them. Scenario: I have a #define for the most negative value a 32-bit integer can hold, and then I try to use this to compare with a floating point value as shown below: #define INT32_MIN (-2147483648L) void main() { float myNumber = 0.0f; if(myNumber > INT32_MIN) { printf("Everything is OK"); } else { printf("The universe is broken!!"); } } Codepad link: http://codepad

How to printf long long

为君一笑 提交于 2019-11-27 09:10:59
I'm doing a program that aproximate PI and i'm trying to use long long, but it isn't working. Here is the code #include<stdio.h> #include<math.h> typedef long long num; main(){ num pi; pi=0; num e, n; scanf("%d", &n); for(e=0; 1;e++){ pi += ((pow((-1.0),e))/(2.0*e+1.0)); if(e%n==0) printf("%15lld -> %1.16lld\n",e, 4*pi); //printf("%lld\n",4*pi); } } karadoc %lld is the standard C99 way, but that doesn't work on the compiler that I'm using (mingw32-gcc v4.6.0). The way to do it on this compiler is: %I64d So try this: if(e%n==0)printf("%15I64d -> %1.16I64d\n",e, 4*pi); and scanf("%I64d", &n);

How to handle arbitrarily large integers

假装没事ソ 提交于 2019-11-27 08:25:14
I'm working on a programming language, and today I got the point where I could compile the factorial function(recursive), however due to the maximum size of an integer the largest I can get is factorial(12). What are some techniques for handling integers of an arbitrary maximum size. The language currently works by translating code to C++. If you need larger than 32-bits you could consider using 64-bit integers (long long), or use or write an arbitrary precision math library, e.g. GNU MP . If you want to roll your own arbitrary precision library, see Knuth's Seminumerical Algorithms, volume 2

How to make Timer countdown along with progress bar?

笑着哭i 提交于 2019-11-27 08:20:35
问题 How can I make it so that the progress bar slowly goes down with the time limit? class GamePanel extends JPanel implements MouseListener, ActionListener { private JButton quit; private JButton q; private Font loadFont; public GamePanel() { setBackground(Color.blue); // sets background color this.setLayout(null); quit = new JButton("Quit"); quit.addActionListener(this); quit.setBounds(550, 700, 100, 30); this.add(quit); q = new JButton("Questions"); q.addActionListener(this); q.setBounds(100,

Generate random values in C#

有些话、适合烂在心里 提交于 2019-11-27 07:25:49
How can I generate random Int64 and UInt64 values using the Random class in C#? Noldorin This should do the trick. (It's an extension method so that you can call it just as you call the normal Next or NextDouble methods on a Random object). public static Int64 NextInt64(this Random rnd) { var buffer = new byte[sizeof(Int64)]; rnd.NextBytes(buffer); return BitConverter.ToInt64(buffer, 0); } Just replace Int64 with UInt64 everywhere if you want unsigned integers instead and all should work fine. Note: Since no context was provided regarding security or the desired randomness of the generated

Isn't an Int64 equal to a long in C#?

两盒软妹~` 提交于 2019-11-27 06:35:03
问题 I have been playing around with SQL and databases in C# via SqlCeConnection. I have been using ExecuteReader to read results and BigInt values for record IDs which are read into Longs. Today I have been playing with SQL statements that use COUNT based statements ('SELECT COUNT(*) FROM X') and have been using ExecuteScalar to read these single valued results. However, I ran into an issue. I can't seem to store the values into a Long data type, which I have been using up to now. I can store

How to use long id in Rails applications?

烈酒焚心 提交于 2019-11-27 06:33:50
How can I change the (default) type for ActiveRecord's IDs? int is not long enough, I would prefer long. I was surprised that there is no :long for the migrations - does one just use some decimal? Credits to http://moeffju.net/blog/using-bigint-columns-in-rails-migrations class CreateDemo < ActiveRecord::Migration def self.up create_table :demo, :id => false do |t| t.integer :id, :limit => 8 end end end See the option :id => false which disables the automatic creation of the id field The t.integer :id, :limit => 8 line will produce a 64 bit integer field To set the default primary key column

How do I convert from int to Long in Java?

那年仲夏 提交于 2019-11-27 05:57:56
I keep finding both on here and Google people having troubles going from long to int and not the other way around. Yet I'm sure I'm not the only one that has run into this scenario before going from int to Long . The only other answers I've found were "Just set it as Long in the first place" which really doesn't address the question. I initially tried casting but I get a " Cannot cast from int to Long " for (int i = 0; i < myArrayList.size(); ++i ) { content = new Content(); content.setDescription(myArrayList.get(i)); content.setSequence((Long) i); session.save(content); } As you can imagine I

Long vs BigInteger

走远了吗. 提交于 2019-11-27 05:38:58
问题 I understand that both java.lang.Long and java.math.BigInteger can hold very large natural numbers. I also know Long's max value, but what is the max value for BigInteger? And aside from capacity, would BigInteger ever perform better when working with generally large integers that still fall in Long's range? Question Is the only consideration: is my value too large for Long? 回答1: BigInteger is capable of holding far bigger numbers than Long. BigInteger seems capable of holding (2 ^ 32) ^