• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to detect and prevent integer overflow when multiplying an integer by float in Java?

 
Marshal
Posts: 74041
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tan Quang wrote:. . . if indeed java supports unsigned data types like other programming languages . . .

It doesn't. That has been a point of controversy for twenty‑five years, and I can't envisage unsigned ever being introduced.
Go through the Integer and Long classes and look for anything with unsignedXXX in its name. If that doesn't help, you may have to convert your int unsigneds into longs, and your bigint unsigneds into BigIntegers.


[Additional] We had a bit of a discussion about unsigned integer types recently: here.
 
Saloon Keeper
Posts: 1306
40
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am curious, do you actually need to deal with values larger than Integer.MAX_VALUE in practice?

I occasionally use long data types in Java, and we are starting to see them in more places where historically we wouldn't.

On the other hand, I programmed daily for decades scarcely ever using any integral types larger than a C/C++ long, which at that time was almost always a 32-bit quantity.

 
Greenhorn
Posts: 24
jQuery MySQL Database PHP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesse Silverman wrote:Java still does not allow you to declare the type of a primitive as unsigned, nor are there UnsignedInteger and associated other wrapper types defined.

There are a great number of methods (far too many to list here, in fact), in both the Integer and Long classes that have been added somewhat recently, mostly in Java 8 and 9 that make working with unsigned values much easier, please refer:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Integer.html

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Long.html

So you can't declare a Data Type to be unsigned, but for operations, you sure can specify to treat the operands and results as unsigned.

I spent 11+ years working on a giant multi-lingual product (really more of a platform, actually) that had the same values represented in C/C++, SQL, C# and Java.

There was more than a little pain around this (okay, sometimes a LOT) and these methods that are now part of the standard wrapper classes could probably have helped us some.

The lack of there being a way to actually declare a Java VALUE unsigned still would cause us a lot of headaches even today, and I think we would still try to avoid exposing unsigned types to user api's.  Sadly, because we were working with many native and .Net api's, that was not always possible.  Hilarity sometimes ensued.

I will add that actually needing the full RANGE of unsigned long is spectacularly uncommon.  I feel like 100% of the use cases I would be using the methods found in the provided links would be for compatibility with non-Java API's, but I may be forgetting something.

When working in Pure Java (a luxury I rarely have had) I would generally avoid unsigned types wherever possible, it is not a good state of affairs to be having values that need to be reminded that they are indeed unsigned every time one operates on or compares them.



Thank you, it's thanks to you that I came across something called Pure Java. I hope to work with it at least once in my life before switching to another programming language.
As for this topic, I don't think there are any perfect answers anymore, so I think it's over.
Many thanks for the helpful answer.
 
Greenhorn
Posts: 3
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think, it should work in this way:

 
Campbell Ritchie
Marshal
Posts: 74041
332
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would much prefer to throw an exception than print error messages and still return an incorrect value. I don't think line 8 will ever be reached.
I would also prefer never to see floats again . And nobody seems to have taken the possibility of a NaN being passed into consideration.
This all shows some of the drawbacks of binary floating‑point arithmetic.
 
Jesse Silverman
Saloon Keeper
Posts: 1306
40
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Campbell said.

I think I am done with this thread too.

Lots of interesting and important things got mentioned in it, but also a lot of silly ones that lost the spirit of the original webpage the poster was working from.

The last note before I forget is that "Pure Java" referred not to any special version of the language, but rather to Java running only Java, without interacting with API's using unsigned integer quantities in them because they were written in C/C++, FORTRAN, Go or what have you.  I rarely got to work with "Pure Java" because we were always interacting with API's written in one or more of those other languages.

As described, unsigned data coming from those was awkward and clunky to work with in Java, a little less so now.

Anyway, a lot of good stuff was discussed in here, hopefully future readers will focus on that stuff and not the bits that got plain silly.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic